window.go 8.3 KB

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