~melchizedek6809/WolkenWelten

ref: 78258f0cc2def46a32d208cb28857eba5047a074 WolkenWelten/client/src/mods/z_blockchooser.nuj -rw-r--r-- 4.5 KiB
78258f0cBen (Win10) UI Work 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
[def blockchooser/active #f]
[def blockchooser/selection 2]
[def blockchooser/w/label #nil]
[def blockchooser/w/sprite #nil]
[def blockchooser/w/popup #nil]

[defun blockchooser/refresh/widgets []
  [when-not blockchooser/w/label
            [blockchooser/w/init!]]
  [widget/val/int! blockchooser/w/sprite blockchooser/selection]
  [def name [block/name blockchooser/selection]]
  [widget/label! blockchooser/w/label [fmt "{name}"]]
  [cons-mode/block! blockchooser/selection]]

[defun blockchooser/remove/widgets []
  [when blockchooser/w/label
    [widget/parent! blockchooser/w/label #nil]
    [set! blockchooser/w/label #nil]]
  [when blockchooser/w/sprite
    [widget/parent! blockchooser/w/sprite #nil]
    [set! blockchooser/w/label #nil]]]

[defun blockchooser/refresh []
  [if blockchooser/active
      [blockchooser/refresh/widgets]
      [blockchooser/remove/widgets]]]

[defun blockchooser/hide! []
  [widget/parent! blockchooser/w/popup #nil]
  [set! blockchooser/w/popup #nil]
  [widget/focus! w-game-screen]]

[defun blockchooser/show! []
  [def h [+ 64 16 [* [/ [block/count] 8] 72]]]
  [set! blockchooser/w/popup [widget [ :panel
                                       :width 584
                                       :height h
                                       :val 8
                                       :center
                                       [ :label
                                         :label "Block chooser"
                                         :big
                                         :width -1
                                         :height 64]]]]
  [def hover-info [widget [ :label
                            :parent blockchooser/w/popup
                            :y 40
                            :width -1
                            :height 32]]]
  [for [x 0 8]
       [for [y 0 8]
            [let [[i [+ x [* 8 y]]]]
              [when [and [> i 0] [< i [block/count]]]
                [widget [ :button
                          :parent blockchooser/w/popup
                          :x [* x 72]
                          :y [+ 64 [* y 72]]
                          :width 64
                          :height 64
                          :focussed
                          [ :sprite
                            :x 4
                            :y 4
                            :width -9
                            :height -9
                            :val i]
                          :on-hover [\ []
                                     [widget/label! hover-info [block/name i]]]
                          :on-click [\ []
                                     [set! player/pear-shooter-active #f]
                                     [set! blockchooser/selection [- i 1]]
                                     [blockchooser/refresh]]]]]]]]]

[defun blockchooser/toggle! []
  "Toggle the visibility of the inventory popup"
  [if blockchooser/w/popup
      [blockchooser/hide!]
      [blockchooser/show!]]]

[defun blockchooser/w/init! []
  [macro-panel/init!]
  [set! blockchooser/w/label [widget [ :label
                                       :parent macro-panel
                                       :width -1
                                       :height 48
                                       :label ""]]]
  [set! blockchooser/w/sprite [widget [:sprite
                                       :parent macro-panel
                                       :x 24
                                       :y 32
                                       :width 64
                                       :height 64
                                       :val 0]]]
  [widget/bind w-game-screen "focus" blockchooser/hide!]]

[defun blockchooser/select! [sel]
       "Select a particular block"
       [when [widget-focus-on-game?]
             [set! blockchooser/selection sel]
             [when [>= blockchooser/selection [block/count]]
                   [set! blockchooser/selection 1]]
             [when [< blockchooser/selection 1]
                   [set! blockchooser/selection [- [block/count] 1]]]
             [blockchooser/refresh]]]

[defun blockchooser/select-next! []
       "Selects the next item from a players inventory"
       [blockchooser/select! [+ blockchooser/selection 1]]]

[defun blockchooser/select-prev! []
       "Selects the previous item from a players inventory"
       [blockchooser/select! [- blockchooser/selection 1]]]

[defun blockchooser/place! []
      [player-place-block! blockchooser/selection]]

[event-bind on-join :blockchooser blockchooser/w/init!]