window.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/math32"
  7. "github.com/g3n/engine/window"
  8. )
  9. /*********************************************
  10. Window panel
  11. +-----------------------------------------+
  12. | Title panel |
  13. +-----------------------------------------+
  14. | Content panel |
  15. | +-----------------------------------+ |
  16. | | | |
  17. | | | |
  18. | | | |
  19. | | | |
  20. | | | |
  21. | | | |
  22. | +-----------------------------------+ |
  23. | |
  24. +-----------------------------------------+
  25. *********************************************/
  26. // Window represents a window GUI element
  27. type Window struct {
  28. Panel // Embedded Panel
  29. styles *WindowStyles
  30. title *WindowTitle // internal optional title panel
  31. client Panel // internal client panel
  32. resizable ResizeBorders
  33. overBorder string
  34. drag bool
  35. mouseX float32
  36. mouseY float32
  37. }
  38. // WindowStyle contains the styling of a Window
  39. type WindowStyle struct {
  40. Border RectBounds
  41. Paddings RectBounds
  42. BorderColor math32.Color4
  43. TitleBorders RectBounds
  44. TitleBorderColor math32.Color4
  45. TitleBgColor math32.Color4
  46. TitleFgColor math32.Color4
  47. }
  48. // WindowStyles contains a WindowStyle for each valid GUI state
  49. type WindowStyles struct {
  50. Normal WindowStyle
  51. Over WindowStyle
  52. Focus WindowStyle
  53. Disabled WindowStyle
  54. }
  55. type ResizeBorders int
  56. const (
  57. ResizeTop = ResizeBorders(1 << (iota + 1))
  58. ResizeRight
  59. ResizeBottom
  60. ResizeLeft
  61. ResizeAll = ResizeTop | ResizeRight | ResizeBottom | ResizeLeft
  62. )
  63. // NewWindow creates and returns a pointer to a new window with the
  64. // specified dimensions
  65. func NewWindow(width, height float32) *Window {
  66. w := new(Window)
  67. w.styles = &StyleDefault().Window
  68. w.Panel.Initialize(width, height)
  69. w.Panel.Subscribe(OnMouseDown, w.onMouse)
  70. w.Panel.Subscribe(OnMouseUp, w.onMouse)
  71. w.Panel.Subscribe(OnCursor, w.onCursor)
  72. w.Panel.Subscribe(OnCursorEnter, w.onCursor)
  73. w.Panel.Subscribe(OnCursorLeave, w.onCursor)
  74. w.Panel.Subscribe(OnResize, func(evname string, ev interface{}) { w.recalc() })
  75. w.client.Initialize(0, 0)
  76. w.Panel.Add(&w.client)
  77. w.recalc()
  78. w.update()
  79. return w
  80. }
  81. // SetResizable set the borders which are resizable
  82. func (w *Window) SetResizable(res ResizeBorders) {
  83. w.resizable = res
  84. }
  85. // SetTitle sets the title of this window
  86. func (w *Window) SetTitle(text string) {
  87. if w.title == nil {
  88. w.title = newWindowTitle(w, text)
  89. w.Panel.Add(w.title)
  90. } else {
  91. w.title.label.SetText(text)
  92. }
  93. w.update()
  94. w.recalc()
  95. }
  96. // Add adds a child panel to the client area of this window
  97. func (w *Window) Add(ichild IPanel) *Window {
  98. w.client.Add(ichild)
  99. return w
  100. }
  101. // SetLayout set the layout of this window content area
  102. func (w *Window) SetLayout(layout ILayout) {
  103. w.client.SetLayout(layout)
  104. }
  105. // onMouse process subscribed mouse events over the window
  106. func (w *Window) onMouse(evname string, ev interface{}) {
  107. mev := ev.(*window.MouseEvent)
  108. switch evname {
  109. case OnMouseDown:
  110. par := w.Parent().(IPanel).GetPanel()
  111. par.SetTopChild(w)
  112. if w.overBorder != "" {
  113. w.drag = true
  114. w.mouseX = mev.Xpos
  115. w.mouseY = mev.Ypos
  116. w.root.SetMouseFocus(w)
  117. }
  118. case OnMouseUp:
  119. w.drag = false
  120. w.root.SetCursorNormal()
  121. w.root.SetMouseFocus(nil)
  122. default:
  123. return
  124. }
  125. w.root.StopPropagation(StopAll)
  126. }
  127. // onCursor process subscribed cursor events over the window
  128. func (w *Window) onCursor(evname string, ev interface{}) {
  129. if evname == OnCursor {
  130. cev := ev.(*window.CursorEvent)
  131. if !w.drag {
  132. cx := cev.Xpos - w.pospix.X
  133. cy := cev.Ypos - w.pospix.Y
  134. if cy <= w.borderSizes.Top {
  135. if w.resizable&ResizeTop != 0 {
  136. w.overBorder = "top"
  137. w.root.SetCursorVResize()
  138. }
  139. } else if cy >= w.height-w.borderSizes.Bottom {
  140. if w.resizable&ResizeBottom != 0 {
  141. w.overBorder = "bottom"
  142. w.root.SetCursorVResize()
  143. }
  144. } else if cx <= w.borderSizes.Left {
  145. if w.resizable&ResizeLeft != 0 {
  146. w.overBorder = "left"
  147. w.root.SetCursorHResize()
  148. }
  149. } else if cx >= w.width-w.borderSizes.Right {
  150. if w.resizable&ResizeRight != 0 {
  151. w.overBorder = "right"
  152. w.root.SetCursorHResize()
  153. }
  154. } else {
  155. if w.overBorder != "" {
  156. w.root.SetCursorNormal()
  157. w.overBorder = ""
  158. }
  159. }
  160. } else {
  161. switch w.overBorder {
  162. case "top":
  163. delta := cev.Ypos - w.mouseY
  164. w.mouseY = cev.Ypos
  165. newHeight := w.Height() - delta
  166. if newHeight < w.MinHeight() {
  167. return
  168. }
  169. w.SetPositionY(w.Position().Y + delta)
  170. w.SetHeight(newHeight)
  171. case "right":
  172. delta := cev.Xpos - w.mouseX
  173. w.mouseX = cev.Xpos
  174. newWidth := w.Width() + delta
  175. w.SetWidth(newWidth)
  176. case "bottom":
  177. delta := cev.Ypos - w.mouseY
  178. w.mouseY = cev.Ypos
  179. newHeight := w.Height() + delta
  180. w.SetHeight(newHeight)
  181. case "left":
  182. delta := cev.Xpos - w.mouseX
  183. w.mouseX = cev.Xpos
  184. newWidth := w.Width() - delta
  185. if newWidth < w.MinWidth() {
  186. return
  187. }
  188. w.SetPositionX(w.Position().X + delta)
  189. w.SetWidth(newWidth)
  190. }
  191. }
  192. } else if evname == OnCursorLeave {
  193. if !w.drag {
  194. w.root.SetCursorNormal()
  195. }
  196. }
  197. w.root.StopPropagation(StopAll)
  198. }
  199. // update updates the button visual state
  200. func (w *Window) update() {
  201. if !w.Enabled() {
  202. w.applyStyle(&w.styles.Disabled)
  203. return
  204. }
  205. w.applyStyle(&w.styles.Normal)
  206. }
  207. func (w *Window) applyStyle(s *WindowStyle) {
  208. w.SetBordersColor4(&s.BorderColor)
  209. w.SetBordersFrom(&s.Border)
  210. w.SetPaddingsFrom(&s.Paddings)
  211. if w.title != nil {
  212. w.title.applyStyle(s)
  213. }
  214. }
  215. // recalc recalculates the sizes and positions of the internal panels
  216. // from the outside to the inside.
  217. func (w *Window) recalc() {
  218. // Window title
  219. height := w.content.Height
  220. width := w.content.Width
  221. cx := float32(0)
  222. cy := float32(0)
  223. if w.title != nil {
  224. w.title.SetWidth(w.content.Width)
  225. w.title.recalc()
  226. height -= w.title.height
  227. cy = w.title.height
  228. }
  229. // Content area
  230. w.client.SetPosition(cx, cy)
  231. w.client.SetSize(width, height)
  232. }
  233. // WindowTitle represents the title bar of a Window
  234. type WindowTitle struct {
  235. Panel // Embedded panel
  236. win *Window
  237. label Label
  238. pressed bool
  239. drag bool
  240. mouseX float32
  241. mouseY float32
  242. }
  243. // newWindowTitle creates and returns a pointer to a window title panel
  244. func newWindowTitle(win *Window, text string) *WindowTitle {
  245. wt := new(WindowTitle)
  246. wt.win = win
  247. wt.Panel.Initialize(0, 0)
  248. wt.label.initialize(text, StyleDefault().Font)
  249. wt.Panel.Add(&wt.label)
  250. wt.Subscribe(OnMouseDown, wt.onMouse)
  251. wt.Subscribe(OnMouseUp, wt.onMouse)
  252. wt.Subscribe(OnCursor, wt.onCursor)
  253. wt.Subscribe(OnCursorEnter, wt.onCursor)
  254. wt.Subscribe(OnCursorLeave, wt.onCursor)
  255. wt.recalc()
  256. return wt
  257. }
  258. // onMouse process subscribed mouse button events over the window title
  259. func (wt *WindowTitle) onMouse(evname string, ev interface{}) {
  260. mev := ev.(*window.MouseEvent)
  261. switch evname {
  262. case OnMouseDown:
  263. wt.pressed = true
  264. wt.mouseX = mev.Xpos
  265. wt.mouseY = mev.Ypos
  266. wt.win.root.SetMouseFocus(wt)
  267. case OnMouseUp:
  268. wt.pressed = false
  269. wt.win.root.SetMouseFocus(nil)
  270. default:
  271. return
  272. }
  273. wt.win.root.StopPropagation(Stop3D)
  274. }
  275. // onCursor process subscribed cursor events over the window title
  276. func (wt *WindowTitle) onCursor(evname string, ev interface{}) {
  277. if evname == OnCursorEnter {
  278. wt.win.root.SetCursorDrag()
  279. } else if evname == OnCursorLeave {
  280. wt.win.root.SetCursorNormal()
  281. } else if evname == OnCursor {
  282. if !wt.pressed {
  283. wt.win.root.StopPropagation(Stop3D)
  284. return
  285. }
  286. cev := ev.(*window.CursorEvent)
  287. dy := wt.mouseY - cev.Ypos
  288. dx := wt.mouseX - cev.Xpos
  289. wt.mouseX = cev.Xpos
  290. wt.mouseY = cev.Ypos
  291. posX := wt.win.Position().X - dx
  292. posY := wt.win.Position().Y - dy
  293. wt.win.SetPosition(posX, posY)
  294. }
  295. wt.win.root.StopPropagation(Stop3D)
  296. }
  297. // applyStyles sets the specified window title style
  298. func (wt *WindowTitle) applyStyle(s *WindowStyle) {
  299. wt.SetBordersFrom(&s.TitleBorders)
  300. wt.SetBordersColor4(&s.TitleBorderColor)
  301. wt.SetColor4(&s.TitleBgColor)
  302. wt.label.SetColor4(&s.TitleFgColor)
  303. }
  304. // recalc recalculates the height and position of the label in the title bar.
  305. func (wt *WindowTitle) recalc() {
  306. xpos := (wt.width - wt.label.width) / 2
  307. wt.label.SetPositionX(xpos)
  308. wt.SetContentHeight(wt.label.Height())
  309. }