• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Periodic Message Board + Flash

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
This is a project I started a week back, though I did most of the code today.

The code is a complete mess and is not fully MUI yet, (the periodic text is not, rest should be).
I will work more on the code, I don't want to highlight it, however this has not been done before as far as I know.

(gif in 16fps, the text appear smoother in the actual map, the gif is also slightly out of date)
204ac1a3799f9930d3f5883044a65522.gif


https://i.gyazo.com/204ac1a3799f9930d3f5883044a65522.gif
The text is created by using # in the string.
JASS:
//! zinc
    library DEMO requires Board, BoardMessage
    {
        Board b;
        function endOfMsgOne()
        {
            RowMsg.create(b, "Roses are #red#, violets are blue, I got a #random# message for #you#.", 1);
        }
   
        function onZero()
        {
            RowMsg bm;
            b = Board.create();
            b[0][0].width = 0.385;
            b.visible[Player(0)] = true;
       
            bm = RowMsg.create(b, "hello! this is a test #message# which should appear periodically.", 0);
            bm.callback = endOfMsgOne;
        }
   
        function onInit()
        {
            TimerStart(CreateTimer(), 0, false, function onZero);
        }
    }
//! endzinc
JASS:
//! zinc
    library BoardMessage requires TimerUtils, Board, ARGB, StrFlash
    {
        type onEnd extends function();
   
        function onEndFunc(onEnd oE)
        {
            oE.evaluate();
        }
   
        public struct RowMsg
        {
            integer currentChar = 1, endChar, row;
            boolean first = false, finished = false;
            string color, colorEnd = "|r", cColor, toWrite, toWriteOriginal;
            timer loopT;
            Board bb;
            ARGB c1, c2;
            onEnd callback;
       
            static method onLoop()
            {
                thistype this = GetTimerData(GetExpiredTimer());
                string char = SubString(toWrite, currentChar, currentChar + 1), pre, end;
           
                if(char == " ")
                {
                    currentChar += 1;
                    char = SubString(toWrite, currentChar, currentChar + 1);
                }
                if(char == "#")
                {
                    pre = SubString(toWrite, 0, currentChar);
                    end = SubString(toWrite, currentChar + 1, StringLength(toWrite));
                    if(!first)
                    {
                        toWrite = pre + color + end;
                        currentChar += StringLength(color);
                        first = true;
                    }
                    else if(first)
                    {
                        toWrite = pre + colorEnd + end;
                        currentChar += StringLength(colorEnd);
                        first = false;
                    }
                }
                bb[0][row].text = SubString(toWrite, 0, currentChar);
                currentChar += 1;
                if(currentChar > StringLength(toWrite))
                {
                    StrFlash.create(toWriteOriginal, bb, row, c1, c2);
                    DestroyTimer(loopT);
                    if(callback != null)
                    {
                        onEndFunc(callback);
                    }
                    this.deallocate();
                }
            }
   
            static method create(Board b, string s, integer r) -> thistype
            {
                thistype this = thistype.allocate();
                bb = b;
                toWrite = s;
                toWriteOriginal = s;
                c1 = ARGB.create(0, 255, 255, 0);
                c2 = ARGB.create(0, 255, 0, 0);   
                row = r;
                color     = c1.GetColor();
                if(r > b.row.count - 1)
                {
                    b.row.count = r;
                    b[0][r].width = 0.385;
                }
           
                loopT = NewTimerEx(this);
                TimerStart(loopT, 0.05, true, function thistype.onLoop);
                return this;
            }
        }
    }
//! endzinc

JASS:
//! zinc
    library StrFlash requires TimerUtils, ARGB, Board
    {
        public struct StrFlash
        {
            integer rdif, gdif, bdif,
                    oR  , oG  , oB,
                    dR  , dG  , dB,
                    cR  , cG  , cB;
       
            timer t;
            integer row;
            string text, otext;
            real time = 0;   
            Board bb;
            ARGB c, o, d;
       
            static method onLoop()
            {
                thistype this = GetTimerData(GetExpiredTimer());
                integer i = 0;
                string pre, end, char;
                boolean first = false;
           
                if(cR != dR)
                {
                    cR += rdif / 30;

                    if(cR > 255){
                        cR = 255;
                    }
                    else if(cR < 0){
                        cR = 0;
                    }
                }
                if(cB != dB)
                {
                    cB += bdif / 30;
                    if(cB > 255){
                        cB = 255;
                    }
                    else if(cB < 0){
                        cB = 0;
                    }
                }
                if(cG != dG)
                {
                    cG += gdif / 30;
                    if(cG > 255){
                        cG = 255;
                    }
                    else if(cG < 0){
                        cG = 0;
                    }
                }
                c = ARGB.create(0, cR, cG, cB);
                while(i < StringLength(text))
                {
                    char = SubString(text, i, i + 1);
                    if(char == "#")
                    {
                        pre = SubString(text, 0, i);
                        end = SubString(text, i + 1, StringLength(text));
                        if(!first)
                        {
                            text = pre + c.GetColor() + end;
                            i += StringLength(c.GetColor());
                            first = true;
                        }
                        else if(first)
                        {
                            text = pre + "|r" + end;
                            i += 2;
                            first = false;
                        }
                        bb[0][row].text = text;
                    }
                    i += 1;
                }
                text = otext;
                if(cB == dB && cR == dR && cG == dG)
                {
                    ReleaseTimer(t);
                    thistype.create(otext, bb, row, d, o);
                    this.destroy();
                }
            }
       
            static method create(string s, Board b, integer r, ARGB c1, ARGB c2) -> thistype
            {
                thistype this = thistype.allocate();
           
                o = c1;
                d = c2;
           
                oR = c1.red;
                oG = c1.green;
                oB = c1.blue;
           
                cR = oR;
                cG = oG;
                cB = oB;
           
                dR = c2.red;
                dG = c2.green;
                dB = c2.blue;
           
                rdif = dR - oR;
                gdif = dG - oG;
                bdif = dB - oB;
           
                text = s;
                otext = s;
                row = r;
           
                bb = b;
           
                t = NewTimerEx(this);
                TimerStart(t, 0.03, true, function thistype.onLoop);
                return this;
            }
        }
    }
//! endzinc
 
Last edited:

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
All I found was a bunch of speed jerks whining about how slow it was.
(almost read the first two pages)
I could not care less. I admit that I don't like the ARGB syntax though, creating an object instance to return a color feels weird.

But more importantly, I need ARGB for the multiboard lib to work so if I want to switch out ARGB I need to switch that as well, and I actually like the multiboard syntax.
 
Level 13
Joined
Nov 7, 2014
Messages
571
All I found was a bunch of speed jerks whining about how slow it was.

StringColors.j:
JASS:
public static method convert takes integer alpha, integer red, integer green, integer blue returns string
    return "|c" + hexChars[alpha] + hexChars[red] + hexChars[green] + hexChars[blue]
endmethod

static method convertHex takes integer hex returns string
    local integer alpha
    local integer red
    local integer green
    local integer blue

    if (0 > hex) then
        set hex = -(-hex + 2147483648)
        set alpha = 128 + hex/16777216
        set hex = hex - (alpha - 128)*16777216
    else
        set alpha = hex/16777216
        set hex = hex - alpha*16777216
    endif

    set red = hex/65536

    set hex = hex - red*65536
    set green = hex/256
    set blue = hex - green*256

    return convert(alpha, red, green, blue)
endmethod
Nestharus seems to have copied Vex's code without thinking much, only converting the constants from hex to decimal... lol =)
Here's a faster version (speed-jerk += 1):
JASS:
function i2hex takes integer i returns string
    local integer i2
    local integer A
    local integer R
    local integer G
    local integer B

    if i < 0 then
        set i = i + 0x80000000

        set i2 = i / 0x100
        set B = i -  i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set G = i - i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set R = i - i2 * 0x100

        set A = 0x80 + i / 0x100
    else

        set i2 = i / 0x100
        set B = i -  i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set G = i - i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set R = i - i2 * 0x100

        set A = i / 0x100
    endif

    return b2h[A] + b2h[R] + b2h[G] + b2h[B]
endfunction
as a lib:
JASS:
library i2hex initializer byte_to_hex_init

globals
    private string array b2h
endglobals
private function byte_to_hex_init takes nothing returns nothing
    local integer d0 = 16
    local integer d1

    set b2h[0] = "0"
    set b2h[1] = "1"
    set b2h[2] = "2"
    set b2h[3] = "3"
    set b2h[4] = "4"
    set b2h[5] = "5"
    set b2h[6] = "6"
    set b2h[7] = "7"
    set b2h[8] = "8"
    set b2h[9] = "9"
    set b2h[10] = "A"
    set b2h[11] = "B"
    set b2h[12] = "C"
    set b2h[13] = "D"
    set b2h[14] = "E"
    set b2h[15] = "F"

    loop
        set d0 = d0 - 1
        set d1 = 16
        loop
            set d1 = d1 - 1
            set b2h[d0*16+d1] = b2h[d0] + b2h[d1]
            exitwhen d1 == 0
        endloop
        exitwhen d0 == 0
    endloop
endfunction

function i2hex takes integer i returns string
    local integer i2
    local integer A
    local integer R
    local integer G
    local integer B

    if i < 0 then
        set i = i + 0x80000000

        set i2 = i / 0x100
        set B = i -  i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set G = i - i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set R = i - i2 * 0x100

        set A = 0x80 + i / 0x100
    else

        set i2 = i / 0x100
        set B = i -  i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set G = i - i2 * 0x100
        set i = i2

        set i2 = i / 0x100
        set R = i - i2 * 0x100

        set A = i / 0x100
    endif

    return b2h[A] + b2h[R] + b2h[G] + b2h[B]
endfunction

endlibrary

library demo initializer init requires i2hex

function init takes nothing returns nothing
    call BJDebugMsg(i2hex(0x00000000))
    call BJDebugMsg(i2hex(0x7FFFFFFF))
    call BJDebugMsg(i2hex(0xFFFFFFFF))
    call BJDebugMsg(i2hex(0xAABBCCDD))
    call BJDebugMsg(i2hex(0xAA0B0C0D))
    call BJDebugMsg(i2hex(0x0A0B0C0D))
endfunction

endlibrary

Edit: Okay I was wrong =)
this should be the fastest (it's Vex's algorithm without the silly sign bit unset):
JASS:
function i2hex takes integer i returns string
    local integer A
    local integer R
    local integer G
    local integer B

    if i < 0 then
        set i = i + 0x80000000
        set A = 0x80 + i / 0x1000000
        set i = i - (A - 0x80) * 0x1000000
    else
        set A = i / 0x1000000
        set i = i - A * 0x1000000
    endif
    set R = i / 0x10000
    set i = i - R * 0x10000
    set G = i / 0x100
    set B = i - G * 0x100

    return b2h[A] + b2h[R] + b2h[G] + b2h[B]
endfunction
 
Last edited:
Status
Not open for further replies.
Top