Two Blue
Join Date: Mar 2003
Posts: 2,179
|
Board
Multiboard Decorators
Version 0.2.01
IntroductionBoard is written in vJass and requires the NewGen editor, or Jass Helper in some other configuration. Board requires the latest version of Jass Helper.
Board is a library that eases the work of dealing with multiboards. It provides simple decorators around the natives, which allow for simpler code. There is a speed penalty associated with these decorators, as compared to using the multiboard natives.
Note that these decorators do not limit the number of rows or columns you can have in a multiboard. You can have dozens of Board objects with hundreds of cells each, and this system will work just fine.
Documentation of the API is available in the library's documentation.
Credits
The Board Library
Requirements:
To add Board to your map, copy and paste the below library in to a custom-text trigger in your map.
 Board: library Board requires ARGB
private constant function Mod takes integer dividend, integer divisor returns integer
return dividend - (dividend / divisor) * divisor
endfunction
private constant function B2S takes boolean b returns string
if b then
return "true"
endif
return "false"
endfunction
private function ErrorMsg takes string what, string error returns nothing
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Board Error: " + what + ": " + error)
endfunction
private keyword bBoard
private keyword bColCount
private keyword bRowCount
private keyword bTemp
private keyword bVisibleFor
struct BoardItem extends array
public method operator x takes nothing returns integer
return Mod(Mod(integer(this), 10000), 100)
endmethod
public method operator y takes nothing returns integer
return (Mod(integer(this), 10000) - Mod(Mod(integer(this), 10000), 100)) / 100
endmethod
public method operator text= takes string val returns nothing
local integer k = Mod(integer(this), 10000)
local Board board = Board((integer(this) - k) / 10000)
local integer row = (k - Mod(k, 100)) / 100
local integer col = Mod(k, 100)
local multiboarditem mbi
if board.bBoard == null then
debug call ErrorMsg("BoardItem[" + I2S(col) + "][" + I2S(row) + "].text=", "The board this item belongs to does not exist anymore")
return
endif
if row >= board.bRowCount then
debug call ErrorMsg("BoardItem.text=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, row + 1)
set board.bRowCount = row + 1
endif
if col >= board.bColCount then
debug call ErrorMsg("BoardItem.text=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetColumnCount(board.bBoard, col + 1)
set board.bColCount = col + 1
endif
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemValue(mbi, val)
call MultiboardReleaseItem(mbi)
set mbi = null
endmethod
public method operator icon= takes string val returns nothing
local integer k = Mod(integer(this), 10000)
local Board board = Board((integer(this) - k) / 10000)
local integer row = (k - Mod(k, 100)) / 100
local integer col = Mod(k, 100)
local multiboarditem mbi
if board.bBoard == null then
debug call ErrorMsg("BoardItem[" + I2S(col) + "][" + I2S(row) + "].icon=", "The board this item belongs to does not exist anymore")
return
endif
if row >= board.bRowCount then
debug call ErrorMsg("BoardItem.icon=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, row + 1)
set board.bRowCount = row + 1
endif
if col >= board.bColCount then
debug call ErrorMsg("BoardItem.icon=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetColumnCount(board.bBoard, col + 1)
set board.bColCount = col + 1
endif
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemIcon(mbi, val)
call MultiboardReleaseItem(mbi)
set mbi = null
endmethod
public method operator color= takes ARGB c returns nothing
local integer k = Mod(integer(this), 10000)
local Board board = Board((integer(this) - k) / 10000)
local integer row = (k - Mod(k, 100)) / 100
local integer col = Mod(k, 100)
local multiboarditem mbi
if board.bBoard == null then
debug call ErrorMsg("BoardItem[" + I2S(col) + "][" + I2S(row) + "].color=", "The board this item belongs to does not exist anymore")
return
endif
if row >= board.bRowCount then
debug call ErrorMsg("BoardItem.color=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, row + 1)
set board.bRowCount = row + 1
endif
if col >= board.bColCount then
debug call ErrorMsg("BoardItem.color=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetColumnCount(board.bBoard, col + 1)
set board.bColCount = col + 1
endif
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemValueColor(mbi, c.red, c.green, c.blue, c.alpha)
call MultiboardReleaseItem(mbi)
set mbi = null
endmethod
public method operator width= takes real r returns nothing
local integer k = Mod(integer(this), 10000)
local Board board = Board((integer(this) - k) / 10000)
local integer row = (k - Mod(k, 100)) / 100
local integer col = Mod(k, 100)
local multiboarditem mbi
if board.bBoard == null then
debug call ErrorMsg("BoardItem[" + I2S(col) + "][" + I2S(row) + "].width=", "The board this item belongs to does not exist anymore")
return
endif
if row >= board.bRowCount then
debug call ErrorMsg("BoardItem.width=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, row + 1)
set board.bRowCount = row + 1
endif
if col >= board.bColCount then
debug call ErrorMsg("BoardItem.width=", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetColumnCount(board.bBoard, col + 1)
set board.bColCount = col + 1
endif
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemWidth(mbi, r)
call MultiboardReleaseItem(mbi)
set mbi = null
endmethod
public method setDisplay takes boolean text, boolean icon returns nothing
local integer k = Mod(integer(this), 10000)
local Board board = Board((integer(this) - k) / 10000)
local integer row = (k - Mod(k, 100)) / 100
local integer col = Mod(k, 100)
local multiboarditem mbi
if board.bBoard == null then
debug call ErrorMsg("BoardItem[" + I2S(col) + "][" + I2S(row) + "].setDisplay()", "The board this item belongs to does not exist anymore")
return
endif
if row >= board.bRowCount then
debug call ErrorMsg("BoardItem.setDisplay()", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, row + 1)
set board.bRowCount = row + 1
endif
if col >= board.bColCount then
debug call ErrorMsg("BoardItem.setDisplay()", "The board has been shrunk to no longer include this item. Will resize the board.")
call MultiboardSetColumnCount(board.bBoard, col + 1)
set board.bColCount = col + 1
endif
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemStyle(mbi, text, icon)
call MultiboardReleaseItem(mbi)
set mbi = null
endmethod
endstruct
//! textmacro Board_DeclareRowCol takes I, K, NAME, COUNT_FUNC, OTHER, THIS
struct Board$NAME$ extends array
public method operator [] takes integer $K$ returns BoardItem
local Board board = ((integer(this) - Mod(integer(this), 100)) / 100)
local integer $I$ = Mod(integer(this), 100)
if $K$ >= board.b$OTHER$Count then
call MultiboardSet$COUNT_FUNC$Count(board.bBoard, $K$ + 1)
set board.b$OTHER$Count = $K$ + 1
endif
return BoardItem(integer(board) * 10000 + $I$ * 100 + $K$)
endmethod
public method operator position takes nothing returns integer
return Mod(integer(this), 100)
endmethod
public method operator text= takes string val returns nothing
local Board board = Board((integer(this) - Mod(integer(this), 100)) / 100)
local integer $I$ = thistype(Mod(integer(this), 100))
local multiboarditem mbi
local integer $K$ = 0
if board.bBoard == null then
debug call ErrorMsg("Board$NAME$[" + I2S(col) + "][" + I2S(row) + "].text=", "The board this $I$ belongs to does not exist anymore")
return
endif
if $I$ >= board.b$THIS$Count then
debug call ErrorMsg("Board$NAME$.text=", "The board has been shrunk to no longer include this $I$. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, $I$ + 1)
set board.b$THIS$Count = $I$ + 1
endif
loop
exitwhen $K$ == board.b$OTHER$Count
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemValue(mbi, val)
call MultiboardReleaseItem(mbi)
set $K$ = $K$ + 1
endloop
set mbi = null
endmethod
public method operator icon= takes string val returns nothing
local Board board = Board((integer(this) - Mod(integer(this), 100)) / 100)
local integer $I$ = thistype(Mod(integer(this), 100))
local multiboarditem mbi
local integer $K$ = 0
if board.bBoard == null then
debug call ErrorMsg("Board$NAME$[" + I2S(col) + "][" + I2S(row) + "].icon=", "The board this $I$ belongs to does not exist anymore")
return
endif
if $I$ >= board.b$THIS$Count then
debug call ErrorMsg("Board$NAME$.icon=", "The board has been shrunk to no longer include this $I$. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, $I$ + 1)
set board.b$THIS$Count = $I$ + 1
endif
loop
exitwhen $K$ == board.b$OTHER$Count
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemIcon(mbi, val)
call MultiboardReleaseItem(mbi)
set $K$ = $K$ + 1
endloop
set mbi = null
endmethod
public method operator color= takes ARGB c returns nothing
local Board board = Board((integer(this) - Mod(integer(this), 100)) / 100)
local integer $I$ = thistype(Mod(integer(this), 100))
local multiboarditem mbi
local integer $K$ = 0
if board.bBoard == null then
debug call ErrorMsg("Board$NAME$[" + I2S(col) + "][" + I2S(row) + "].color=", "The board this $I$ belongs to does not exist anymore")
return
endif
if $I$ >= board.b$THIS$Count then
debug call ErrorMsg("Board$NAME$.color=", "The board has been shrunk to no longer include this $I$. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, $I$ + 1)
set board.b$THIS$Count = $I$ + 1
endif
loop
exitwhen $K$ >= board.b$OTHER$Count
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemValueColor(mbi, c.red, c.green, c.blue, c.alpha)
call MultiboardReleaseItem(mbi)
set $K$ = $K$ + 1
endloop
set mbi = null
endmethod
public method operator width= takes real r returns nothing
local Board board = Board((integer(this) - Mod(integer(this), 100)) / 100)
local integer $I$ = thistype(Mod(integer(this), 100))
local multiboarditem mbi
local integer $K$ = 0
if board.bBoard == null then
debug call ErrorMsg("Board$NAME$[" + I2S(col) + "][" + I2S(row) + "].width=", "The board this $I$ belongs to does not exist anymore")
return
endif
if $I$ >= board.b$THIS$Count then
debug call ErrorMsg("Board$NAME$.width=", "The board has been shrunk to no longer include this $I$. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, $I$ + 1)
set board.b$THIS$Count = $I$ + 1
endif
loop
exitwhen $K$ == board.b$OTHER$Count
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemWidth(mbi, r)
call MultiboardReleaseItem(mbi)
set $K$ = $K$ + 1
endloop
set mbi = null
endmethod
public method setDisplay takes boolean text, boolean icon returns nothing
local Board board = Board((integer(this) - Mod(integer(this), 100)) / 100)
local integer $I$ = thistype(Mod(integer(this), 100))
local multiboarditem mbi
local integer $K$ = 0
if board.bBoard == null then
debug call ErrorMsg("Board$NAME$[" + I2S(col) + "][" + I2S(row) + "].setDisplay()", "The board this $I$ belongs to does not exist anymore")
return
endif
if $I$ >= board.b$THIS$Count then
debug call ErrorMsg("Board$NAME$.setDisplay()", "The board has been shrunk to no longer include this $I$. Will resize the board.")
call MultiboardSetRowCount(board.bBoard, $I$ + 1)
set board.b$THIS$Count = $I$ + 1
endif
loop
exitwhen $K$ == board.b$OTHER$Count
set mbi = MultiboardGetItem(board.bBoard, row, col)
call MultiboardSetItemStyle(mbi, text, icon)
call MultiboardReleaseItem(mbi)
set $K$ = $K$ + 1
endloop
set mbi = null
endmethod
endstruct
//! endtextmacro
//! runtextmacro Board_DeclareRowCol("row", "col", "Row", "Column", "Col", "Row")
//! runtextmacro Board_DeclareRowCol("col", "row", "Column", "Row", "Row", "Col")
private struct ItemHelper extends array
public method operator[] takes integer row returns BoardItem
local integer col = Board.bTemp
if row < 0 then
debug call ErrorMsg("Board[" + I2S(col) + "][" + I2S(row) + "]", "Given row id is less than 0")
return 0
elseif col < 0 then
debug call ErrorMsg("Board[" + I2S(col) + "][" + I2S(row) + "]", "Given col id is less than 0")
return 0
endif
if col >= Board(this).bColCount then
call MultiboardSetColumnCount(Board(this).bBoard, col + 1)
set Board(this).bColCount = col + 1
endif
if row >= Board(this).bRowCount then
call MultiboardSetRowCount(Board(this).bBoard, row + 1)
set Board(this).bRowCount = row + 1
endif
return integer(this) * 10000 + row * 100 + Board(this).bTemp
endmethod
endstruct
private struct RowHelper extends array
public method operator count takes nothing returns integer
return Board(this).bRowCount
endmethod
public method operator count= takes integer i returns nothing
call MultiboardSetRowCount(Board(this).bBoard, i + 1)
set Board(this).bRowCount = i + 1
endmethod
public method operator[] takes integer row returns BoardRow
if row < 0 then
debug call ErrorMsg("Board.row[" + I2S(row) + "]", "Given row id is less than 0")
return 0
endif
if row >= Board(this).bRowCount then
call MultiboardSetRowCount(Board(this).bBoard, row + 1)
set Board(this).bRowCount = row + 1
endif
if Board(this).bColCount < 1 then
call MultiboardSetColumnCount(Board(this).bBoard, 1)
set Board(this).bColCount = 1
endif
return BoardRow(row + integer(this) * 100)
endmethod
endstruct
private struct ColumnHelper extends array
public method operator count takes nothing returns integer
return Board(this).bColCount
endmethod
public method operator count= takes integer i returns nothing
call MultiboardSetColumnCount(Board(this).bBoard, i + 1)
set Board(this).bColCount = i + 1
endmethod
public method operator[] takes integer col returns BoardColumn
if col < 0 then
debug call ErrorMsg("Board.col[" + I2S(col) + "]", "Given column id is less than 0")
return 0
endif
if col >= Board(this).bColCount then
call MultiboardSetColumnCount(Board(this).bBoard, col + 1)
set Board(this).bColCount = col + 1
endif
if Board(this).bRowCount < 1 then
call MultiboardSetRowCount(Board(this).bBoard, 1)
set Board(this).bRowCount = 1
endif
return BoardColumn(col + integer(this) * 100)
endmethod
endstruct
private struct VisibleHelper extends array
public method operator []= takes player p, boolean b returns nothing
if GetPlayerId(p) > 11 then
debug call ErrorMsg("Board.visible[\"" + GetPlayerName(p) + "\"]=" + B2S(b), "Given player is a neutral player")
return
endif
set Board(this).bVisibleFor[GetPlayerId(p)] = b
if GetLocalPlayer() == p then
call MultiboardDisplay(Board(this).bBoard, b)
endif
endmethod
public method operator [] takes player p returns boolean
if GetPlayerId(p) > 11 then
debug call ErrorMsg("Board.visible[\"" + GetPlayerName(p) + "\"]", "Given player is a neutral player")
return false
endif
return Board(this).bVisibleFor[GetPlayerId(p)]
endmethod
endstruct
private struct MinimizedHelper extends array
public method operator[]= takes player p, boolean b returns nothing
if GetPlayerId(p) > 11 then
debug call ErrorMsg("Board.minimized[\"" + GetPlayerName(p) + "\"]=" + B2S(b), "Given player is a neutral player")
return
endif
if GetLocalPlayer() == p then
call MultiboardMinimize(Board(this).bBoard, b)
endif
endmethod
endstruct
private struct AllHelper extends array
public method operator text= takes string val returns nothing
call MultiboardSetItemsValue(Board(this).bBoard, val)
endmethod
public method operator icon= takes string val returns nothing
call MultiboardSetItemsIcon(Board(this).bBoard, val)
endmethod
public method operator color= takes ARGB c returns nothing
call MultiboardSetItemsValueColor(Board(this).bBoard, c.red, c.green, c.blue, c.alpha)
endmethod
public method operator width= takes real r returns nothing
call MultiboardSetItemsWidth(Board(this).bBoard, r)
endmethod
public method setDisplay takes boolean text, boolean icon returns nothing
call MultiboardSetItemsStyle(Board(this).bBoard, text, icon)
endmethod
endstruct
struct Board
multiboard bBoard
integer bColCount = 0
integer bRowCount = 0
boolean array bVisibleFor[12]
static integer bTemp
public static method create takes nothing returns thistype
local thistype this = allocate()
set bBoard = CreateMultiboard()
call MultiboardSetItemsWidth(bBoard, 0.01)
call MultiboardSetItemsStyle(bBoard, true, false)
return this
endmethod
private method onDestroy takes nothing returns nothing
call DestroyMultiboard(bBoard)
set bBoard = null
endmethod
public method operator[] takes integer column returns ItemHelper
set bTemp = column
return ItemHelper(this)
endmethod
public method operator col takes nothing returns ColumnHelper
return ColumnHelper(this)
endmethod
public method operator row takes nothing returns RowHelper
return RowHelper(this)
endmethod
public method operator all takes nothing returns AllHelper
return AllHelper(this)
endmethod
public method operator title= takes string value returns nothing
call MultiboardSetTitleText(bBoard, value)
endmethod
public method operator titleColor= takes ARGB color returns nothing
call MultiboardSetTitleTextColor(bBoard, color.red, color.green, color.blue, color.alpha)
endmethod
public method operator visible takes nothing returns VisibleHelper
return VisibleHelper(this)
endmethod
public method operator visible= takes boolean b returns nothing
local integer i = 0
loop
exitwhen i == 12
set bVisibleFor[i] = b
set i = i + 1
endloop
call MultiboardDisplay(bBoard, b)
endmethod
public method operator minimized takes nothing returns MinimizedHelper
return MinimizedHelper(this)
endmethod
public method operator minimized= takes boolean b returns nothing
call MultiboardMinimize(bBoard, b)
endmethod
public method clear takes nothing returns nothing
call MultiboardClear(bBoard)
endmethod
endstruct
endlibrary
Change Log
Last edited by Earth-Fury : 04-19-2010 at 06:10 PM.
|