- Joined
- Apr 30, 2011
- Messages
- 359
JASS:
//========================================================================================
//
// Scrolling Message
// -*- overcold_ice -*-
//
// -[*] Requirements:
// - JNGP
// - latest version of JassHelper
// - Ascii
// [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-ascii-190746/[/url]
// - /*New*/Table
// [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//
// Provides functions to make scrolling game messages, as seen on most Credits
//
// -[*] API:
//
// struct ScrollingMsg extends array
//
// static method create takes nothing returns ScrollingMsg
// method destroy takes nothing returns nothing
// allocator and deallocator for the struct
//
// method add takes string Message returns nothing
// add string Message to the struct's string
// located after the last line of the struct's string
//
// method display takes real XOffset, real YOffset, real Velocity, force ToForce returns boolean
// display scrolling message at XOffset and YOffset,
// with the speed of Velocity (line per second),
// shown to ToForce
// if the struct is destroyed when it's still displaying messages,
// it will automatically destroyed when it finishes displaying
//
//========================================================================================
library ScrollingMessage requires /*New*/Table
//====================================================================================
// C A L I B R A T I O N S E C T I O N
//====================================================================================
globals
private constant real UNKNOWN_CHAR_SIZE = 0.010966167
private constant real MSG_LINE_HEIGHT = 0.0675
private constant real MSG_UPDATE_TIME = 0.0325
private constant real MSG_FADING_TIME = 0.0325
endglobals
//====================================================================================
globals
private real array CharSize
private TableArray Table
endglobals
private module Init
private static method onInit takes nothing returns nothing
set CharSize ['A'] = 0.015288462
set CharSize ['B'] = 0.012268519
set CharSize ['C'] = 0.013802083
set CharSize ['D'] = 0.015288462
set CharSize ['E'] = 0.012268519
set CharSize ['F'] = 0.009287383
set CharSize ['G'] = 0.015288462
set CharSize ['H'] = 0.015288462
set CharSize ['I'] = 0.006172360
set CharSize ['J'] = 0.006172360
set CharSize ['K'] = 0.013802083
set CharSize ['L'] = 0.010801630
set CharSize ['M'] = 0.019875000
set CharSize ['N'] = 0.015288462
set CharSize ['O'] = 0.016843220
set CharSize ['P'] = 0.012268519
set CharSize ['Q'] = 0.018402778
set CharSize ['R'] = 0.013802083
set CharSize ['S'] = 0.012268519
set CharSize ['T'] = 0.010801630
set CharSize ['U'] = 0.015288462
set CharSize ['V'] = 0.013802083
set CharSize ['W'] = 0.021603261
set CharSize ['X'] = 0.015288462
set CharSize ['Y'] = 0.013802083
set CharSize ['Z'] = 0.013802083
set CharSize ['a'] = 0.010801630
set CharSize ['b'] = 0.012268519
set CharSize ['c'] = 0.010801630
set CharSize ['d'] = 0.012268519
set CharSize ['e'] = 0.012268519
set CharSize ['f'] = 0.007703488
set CharSize ['g'] = 0.013802083
set CharSize ['h'] = 0.012268519
set CharSize ['i'] = 0.004643692
set CharSize ['j'] = 0.006172360
set CharSize ['k'] = 0.010801630
set CharSize ['l'] = 0.004643692
set CharSize ['m'] = 0.018402778
set CharSize ['n'] = 0.012268519
set CharSize ['o'] = 0.012268519
set CharSize ['p'] = 0.012268519
set CharSize ['q'] = 0.012268519
set CharSize ['r'] = 0.007703488
set CharSize ['s'] = 0.009287383
set CharSize ['t'] = 0.007703488
set CharSize ['u'] = 0.012268519
set CharSize ['v'] = 0.012268519
set CharSize ['w'] = 0.016843220
set CharSize ['x'] = 0.012268519
set CharSize ['y'] = 0.012268519
set CharSize ['z'] = 0.010801630
set CharSize [' '] = 0.006172360
set CharSize ['!'] = 0.004643692
set CharSize ['"'] = 0.006172360
set CharSize ['#'] = 0.013802083
set CharSize ['$'] = 0.010801630
set CharSize ['%'] = 0.015288462
set CharSize ['&'] = 0.016843220
set CharSize [ 39] = 0.003095794
set CharSize ['('] = 0.006172360
set CharSize [')'] = 0.006172360
set CharSize ['*'] = 0.009287383
set CharSize ['+'] = 0.010801630
set CharSize [','] = 0.004643692
set CharSize ['-'] = 0.007703488
set CharSize ['.'] = 0.004643692
set CharSize ['/'] = 0.009287383
set CharSize ['0'] = 0.012268519
set CharSize ['1'] = 0.004643692
set CharSize ['2'] = 0.012268519
set CharSize ['3'] = 0.012268519
set CharSize ['4'] = 0.013802083
set CharSize ['5'] = 0.012268519
set CharSize ['6'] = 0.012268519
set CharSize ['7'] = 0.010801630
set CharSize ['8'] = 0.012268519
set CharSize ['9'] = 0.012268519
set CharSize [':'] = 0.004643692
set CharSize [';'] = 0.004643692
set CharSize ['<'] = 0.010801630
set CharSize ['='] = 0.010801630
set CharSize ['>'] = 0.010801630
set CharSize ['?'] = 0.010801630
set CharSize ['@'] = 0.015288462
set CharSize ['['] = 0.006172360
set CharSize [ 92] = 0.007703488
set CharSize [']'] = 0.006172360
set CharSize ['^'] = 0.010801630
set CharSize ['_'] = 0.010801630
set CharSize ['`'] = 0.007703488
set CharSize ['{'] = 0.006172360
set CharSize ['|'] = 0.004643692
set CharSize ['}'] = 0.006172360
set CharSize ['~'] = 0.010801630
set Table = TableArray [8192]
endmethod
endmodule
private struct Implementer
implement Init
endstruct
private function IsColorCode takes string s returns boolean
local integer l = 1
local integer i = Char2Ascii(StringCase(SubString(s, 0, 1), false))
if i != 99 then
return false
endif
loop
exitwhen l == 9
set i = Char2Ascii(StringCase(SubString(s, l, l + 1), false))
if (i < 48 or i > 57) and (i < 97 or i > 102) then
return false
endif
set l = l + 1
endloop
return true
endfunction
struct ScrollingMsg extends array
private static integer c
private thistype r
private real x
private real y
private real v
private real l
private boolean s
private boolean d
private boolean ad
private integer lc
private integer cl
private static timer t
private static thistype array i
private static integer ic
private method storeMsg takes string s returns nothing
local integer sl = StringLength(s)
local integer ls = 0
local string ss = ""
local real r = 0
local integer l = 0
loop
if l == sl then
set this.lc = this.lc + 1
set Table [this].string [this.lc] = SubString(s, ls, l)
exitwhen true
endif
set ss = SubString(s, l, l + 1)
if ss == "|" then
set ss = SubString(s, l + 1, l + 2)
if IsColorCode(SubString(s, l + 1, l + 10)) then
set l = l + 9
elseif StringCase(ss, false) == "r" then
set l = l + 1
elseif ss == "|" then
set l = l + 1
set r = r + CharSize ['|']
else
set r = r + CharSize ['|']
endif
elseif StringCase(SubString(s, l, l + 2), false) == "\n" then
set this.lc = this.lc + 1
set Table [this].string [this.lc] = SubString(s, ls, l)
set l = l + 2
set ls = l
set r = 0
elseif CharSize [Char2Ascii(ss)] != 0 then
set r = r + CharSize [Char2Ascii(ss)]
else
set r = r + UNKNOWN_CHAR_SIZE
endif
if r > 1 then
set this.lc = this.lc + 1
set Table [this].string [this.lc] = SubString(s, ls, l)
set ls = l
set r = 0
endif
set l = l + 1
endloop
endmethod
static method create takes nothing returns thistype
local thistype this = thistype(0).r
if this == 0 then
set .c = .c + 1
set this = .c
else
set thistype(0).r = this.r
endif
return this
endmethod
method add takes string s returns nothing
call this.storeMsg(s)
endmethod
private static method onExpire takes nothing returns nothing
local thistype this = thistype(0)
local integer l
local integer ll
loop
set this = .i [this + 1]
set this.l = this.l + this.v
set l = R2I(this.l / MSG_LINE_HEIGHT)
set ll = this.cl
set this.cl = l
if l > this.lc then
set l = this.lc
if this.cl == l + 16 and .s then
call ClearTextMessages()
set this.x = 0
set this.y = 0
set this.v = 0
set this.s = false
set this.d = false
set this.l = 0
set this.cl = 0
if this.ad then
call this.destroy()
endif
set .i [this] = .i [.ic]
set .i [.ic] = 0
set .ic = .ic - 1
set this = this - 1
elseif ll != this.cl and .s then
call DisplayTimedTextToPlayer(GetLocalPlayer(), this.x, this.y + this.l - l, 16 * this.v / (MSG_LINE_HEIGHT * MSG_UPDATE_TIME) - MSG_FADING_TIME, " ")
endif
elseif ll != this.cl and .s then
call DisplayTimedTextToPlayer(GetLocalPlayer(), this.x, this.y + this.l - l, 16 * this.v / (MSG_LINE_HEIGHT * MSG_UPDATE_TIME) - MSG_FADING_TIME, Table [this].string [this.cl])
endif
exitwhen this == .ic
endloop
endmethod
method display takes real x, real y, real v, force f returns boolean
if this.d then
return false
endif
set this.x = x
set this.y = y
set this.v = v * MSG_LINE_HEIGHT * MSG_UPDATE_TIME
set this.s = IsPlayerInForce(GetLocalPlayer(), f) or f == null
set this.d = true
set this.l = 0
set this.cl = 1
if .s then
call DisplayTimedTextToPlayer(GetLocalPlayer(), this.x, this.y, 16 * this.v / (MSG_LINE_HEIGHT * MSG_UPDATE_TIME) - MSG_FADING_TIME, Table [this].string [this.cl])
endif
if .ic == 0 then
call TimerStart(.t, MSG_UPDATE_TIME, true, function thistype.onExpire)
endif
set .ic = .ic + 1
set .i [.ic] = this
return true
endmethod
method destroy takes nothing returns nothing
if this.d then
set this.ad = true
else
call Table [this].flush()
set this.lc = 0
set this.ad = false
endif
set this.r = thistype(0).r
set thistype(0).r = this
endmethod
endstruct
endlibrary