button.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import math
  2. import cairo
  3. import yutani
  4. import text_region
  5. import toaru_fonts
  6. def rounded_rectangle(ctx,x,y,w,h,r):
  7. degrees = math.pi / 180
  8. ctx.new_sub_path()
  9. ctx.arc(x + w - r, y + r, r, -90 * degrees, 0 * degrees)
  10. ctx.arc(x + w - r, y + h - r, r, 0 * degrees, 90 * degrees)
  11. ctx.arc(x + r, y + h - r, r, 90 * degrees, 180 * degrees)
  12. ctx.arc(x + r, y + r, r, 180 * degrees, 270 * degrees)
  13. ctx.close_path()
  14. def draw_button(ctx,x,y,w,h,hilight):
  15. """Theme definition for drawing a button."""
  16. ctx.save()
  17. ctx.set_line_cap(cairo.LINE_CAP_ROUND)
  18. ctx.set_line_join(cairo.LINE_JOIN_ROUND)
  19. if hilight == 2:
  20. rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
  21. ctx.set_source_rgba(134/255,173/255,201/255,1.0)
  22. ctx.set_line_width(2)
  23. ctx.stroke()
  24. rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
  25. ctx.set_source_rgba(202/255,211/255,232/255,1.0)
  26. ctx.fill()
  27. else:
  28. rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
  29. ctx.set_source_rgba(44/255,71/255,91/255,29/255)
  30. ctx.set_line_width(4)
  31. ctx.stroke()
  32. rounded_rectangle(ctx, 2 + x, 2 + y, w - 4, h - 4, 2.0)
  33. ctx.set_source_rgba(158/255,169/255,177/255,1.0)
  34. ctx.set_line_width(2)
  35. ctx.stroke()
  36. if hilight == 1:
  37. pat = cairo.LinearGradient(2+x,2+y,2+x,2+y+h-4)
  38. pat.add_color_stop_rgba(0,1,1,1,1)
  39. pat.add_color_stop_rgba(1,229/255,229/255,246/255,1)
  40. rounded_rectangle(ctx,2+x,2+y,w-4,h-4,2.0)
  41. ctx.set_source(pat)
  42. ctx.fill()
  43. pat = cairo.LinearGradient(3+x,3+y,3+x,3+y+h-4)
  44. pat.add_color_stop_rgba(0,252/255,252/255,254/255,1)
  45. pat.add_color_stop_rgba(1,212/255,223/255,251/255,1)
  46. rounded_rectangle(ctx,3+x,3+y,w-5,h-5,2.0)
  47. ctx.set_source(pat)
  48. ctx.fill()
  49. else:
  50. pat = cairo.LinearGradient(2+x,2+y,2+x,2+y+h-4)
  51. pat.add_color_stop_rgba(0,1,1,1,1)
  52. pat.add_color_stop_rgba(1,241/255,241/255,244/255,1)
  53. rounded_rectangle(ctx,2+x,2+y,w-4,h-4,2.0)
  54. ctx.set_source(pat)
  55. ctx.fill()
  56. pat = cairo.LinearGradient(3+x,3+y,3+x,3+y+h-4)
  57. pat.add_color_stop_rgba(0,252/255,252/255,254/255,1)
  58. pat.add_color_stop_rgba(1,223/255,225/255,230/255,1)
  59. rounded_rectangle(ctx,3+x,3+y,w-5,h-5,2.0)
  60. ctx.set_source(pat)
  61. ctx.fill()
  62. ctx.restore()
  63. class Button(object):
  64. def __init__(self, text, callback):
  65. self.text = text
  66. self.callback = callback
  67. self.hilight = 0
  68. self.x, self.y, self.width, self.height = 0,0,0,0
  69. def draw(self, window, ctx, x, y, w, h):
  70. self.x, self.y, self.width, self.height = x, y, w, h
  71. draw_button(ctx,x,y,w,h,self.hilight)
  72. x_, y_ = ctx.user_to_device(x,y)
  73. tr = text_region.TextRegion(int(x_),int(y_),w,h)
  74. tr.set_alignment(2)
  75. tr.set_valignment(2)
  76. tr.set_text(self.text)
  77. tr.draw(window)
  78. def focus_enter(self):
  79. self.hilight = 1
  80. def focus_leave(self):
  81. self.hilight = 0