window.go 8.5 KB

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