2

我正在开发一个简单的 clojure 扫雷游戏,主要是为了稍微练习一下语言。“核心游戏引擎”已准备就绪,现在我想添加一个简单的 UI。我看过跷跷板,到目前为止它工作得很好。

我唯一苦苦挣扎的是我想使用的mig 布局。我的想法是采用以下布局:

 _______________________
[_________NORTH_________]
[   ][             ][   ]
[ W ][    FIXED    ][ E ]
[ E ][    SIZE     ][ A ]
[ S ][    BOARD    ][ S ]
[ T ][             ][ T ]
[___][_____________][___]
[_________SOUTH_________]

在调整窗口大小的情况下,北、西、东和南应该增长。主面板应具有固定大小,其中包含代表游戏板的所有按钮。

在阅读 mig 布局之前,我考虑过使用包含网格面板的边框面板,但正如跷跷板 github 页面所述:

Seesaw 在 seesaw.core/form-panel 函数中基本支持 GridBagLayout。我不推荐使用它,因为它是半生不熟的,不如使用 MigLayout (seesaw.mig) 或 JGoodies (seesaw.forms) 强大。

我想试试 MigLayout。我的第一次尝试:

(defn make-border-panel 
[]
(mig-panel 
 :constraints ["fill" "[grow][][grow]" "[grow][][grow]"]
 :items [ 
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["X"                  "shrink 0"  ]
       ["Minesweeper North"  "dock north"]
       ["W"                  "dock west" ]
       ["E"                  "dock east" ]
       ["Minesweeper South"  "dock south"]
      ]))

更新 1

我暂时忽略北、南、西、东,并执行以下操作以至少获得中心板:

(defn make-button [x y]
   (button :id (format "cell_%d_%d" x y)
           :icon (clojure.java.io/resource "minesweeper/icons/button.png")))


(defn make-board
  [w h]
  (mig-panel 
      :constraints [(str "wrap" w) "[]" "[]" ]
      :items (for [x (range w) y (range h)]  
          (vector (make-button x y) "w 24px!, h 24px!"))))
4

1 回答 1

0

我意识到我的扫雷游戏并不需要 WEST 和 EAST 面板。我的最终布局如下所示:

 _____________
[____NORTH____]
[             ]
[             ]
[   CENTER    ]
[             ]
[_____________]
[____SOUTH____]

Center 本身是另一个 mig 面板,其中包含代表地雷的按钮。clojure 中的代码如下所示:

(defn make-layout
  [rows cols level]
  (mig-panel
   :constraints ["wrap1" "[center]" "[][]" ]
   :items       [[(make-info-panel)]
                [(make-board-panel rows cols level)]]))


(defn make-board-panel
  [rows cols level]
  (let [bg (button-group)]
    (mig-panel
     :constraints [(str "gap 0, wrap" rows) "[]" "[]" ]
     :items       (for [row (range rows) col (range cols)]
                    (vector (make-button row col level bg) "w 24px!, h 24px!")))))
于 2015-09-23T07:27:23.920 回答