SimpleText: Format and display text on map interface Version 1.3

www

Villager
Member
Joined
Nov 6, 2015
Messages
5
Reaction score
1
First Language
Chinese
Code:
/*:
* @plugindesc Display texts on screen. Version: 2.0
* @author Rui
* @help
* ----------------------------------------
* SimpleText Write Text Args
* ----------------------------------------
* write text on screen. all texts will be saved with map, only need to add once.
* 
* Text
*       String without spaces. Apple_tree will be displayed as Apple tree.
* Args
*       eventID: event id which text will be centered at. value of -1 indicates player. ignore this parameter indicates center of screen.
*       offsetX/offsetY: Offset from original position. Default: 0.
*       duration: text display duration. value of 0 or less indicates permanent. default: 0.  
*       moveX/moveY: movement of text. default: 0.
*       isFollow: whether text follows event or not. this parameter is ignored when eventID is ignored. defualt: true  
*       group: texts can be cleard by group. case insensitive. default: ALL.
*       eg. SimpleText Write Player_1 eventID:-1,duration:0,isFollow:true
*       eg. SimpleText Write -1 eventID:-1,duration:1000,isFollow:false,moveY:1
*       eg. SimpleText Write Map_1 offsetY:200
*       parameters are separated by comma. spaces are not allowed.
*       all commands are case insensitive.
*
* ----------------------------------------
* SimpleText Font Args
* ----------------------------------------
* setup font. call this command will not affect texts that have already been created.
* 
* Args
*       font, size, isItalic, color, outlineWidth, outlineColor, opacity
*       eg. SimpleText Font font:GameFont,color:#910010,size:26,outlineColor:rgba(0,0,0,0.5),opacity:255
*       parameters are separated by comma. spaces are not allowed.
*       all commands are case insensitive.
*        
* ----------------------------------------
* SimpleText ClearAll
* SimpleText ClearMap
* SimpleText ClearByEventID eventID
* SimpleText ClearByGroup group
* ----------------------------------------
* clear texts on screen
* Args
*       eventID: clear all texts bind to this event.
*       group: clear all texts bind to this group.
        eg. SimpleText ClearByEventID -1
*       all commands are case insensitive.
*
*
* License: Free for personal and commercial use
*/


(function() {
    //SimpleText
    function SimpleText() {
        this.simpleTextObjList = {};    //dictionary with mapID as key
        this.fontFace = "GameFont";
        this.fontSize = 21;
        this.fontItalic = false;
        this.textColor = "#ffffff";
        this.outlineColor = "rgba(0,0,0,0.5)";
        this.outlineWidth = 2;
        this.opacity = 255;
    }

    SimpleText.prototype.changeFont = function(args) {
        var commandList = args.split(',');

        for (var i = commandList.length - 1; i >= 0; i--) {
            var command = commandList[i].toUpperCase();

            if (command.lastIndexOf("FONT:", 0) === 0) {
                this.fontFace = command.substring("FONT:".length).replace(/_/g, " ");
            }
            else if (command.lastIndexOf("SIZE:", 0) === 0) {
                this.fontSize = parseInt(command.substring("SIZE:".length));
            }
            else if (command.lastIndexOf("ISITALIC:", 0) === 0) {
                this.fontItalic = command.substring("ISITALIC:".length) === "TRUE";
            }
            else if (command.lastIndexOf("COLOR:", 0) === 0) {
                this.textColor = command.substring("COLOR:".length);
            }
            else if (command.lastIndexOf("OUTLINEWIDTH:", 0) === 0) {
                this.outlineWidth = parseInt(command.substring("OUTLINEWIDTH:".length));
            }
            else if (command.lastIndexOf("OUTLINECOLOR:", 0) === 0) {
                this.outlineColor = command.substring("OUTLINECOLOR:".length);
            }
            else if (command.lastIndexOf("OPACITY:", 0) === 0) {
                this.opacity = parseInt(command.substring("OPACITY:".length));
            }
        }
    }

    SimpleText.prototype.getSimpleTextObjListByMapID = function() {
        var mapID = $gameMap.mapId().toString();
        if (DataManager.___simpleText___.simpleTextObjList.hasOwnProperty(mapID)) {
            return DataManager.___simpleText___.simpleTextObjList[mapID];
        }
        else {
            DataManager.___simpleText___.simpleTextObjList[mapID] = [];
            return DataManager.___simpleText___.simpleTextObjList[mapID];
        }        
    };
   
    //SimpleTextObj object
    function SimpleTextObj(text, args) {
        this.text = text.replace(/_/g, " ");
        this.eventID = null;
        this.duration = null;
        this.offsetX = 0;
        this.offsetY = 0;
        this.moveX = 0;
        this.moveY = 0;
        this.isFollow = true;
        this.group = "ALL";

        this.fontFace = DataManager.___simpleText___.fontFace;
        this.fontSize = DataManager.___simpleText___.fontSize;
        this.fontItalic = DataManager.___simpleText___.fontItalic;
        this.textColor = DataManager.___simpleText___.textColor;
        this.outlineColor = DataManager.___simpleText___.outlineColor;
        this.outlineWidth = DataManager.___simpleText___.outlineWidth;
        this.opacity = DataManager.___simpleText___.opacity;

        var commandList = args.split(',');

        for (var i = commandList.length - 1; i >= 0; i--) {
            var command = commandList[i].toUpperCase();

            if (command.lastIndexOf("EVENTID:", 0) === 0) {
                this.eventID = parseInt(command.substring("EVENTID:".length));
            }
            else if (command.lastIndexOf("DURATION:", 0) === 0) {
                this.duration = parseInt(command.substring("DURATION:".length));
                if (this.duration <= 0)
                    this.duration = null;
            }
            else if (command.lastIndexOf("OFFSETX:", 0) === 0) {
                this.offsetX = parseInt(command.substring("OFFSETX:".length));
            }
            else if (command.lastIndexOf("OFFSETY:", 0) === 0) {
                this.offsetY = parseInt(command.substring("OFFSETY:".length));
                this.offsetY = this.offsetY * -1;
            }
            else if (command.lastIndexOf("MOVEX:", 0) === 0) {
                this.moveX = parseInt(command.substring("MOVEX:".length));
            }
            else if (command.lastIndexOf("MOVEY:", 0) === 0) {
                this.moveY = parseInt(command.substring("MOVEY:".length));
                this.moveY = this.moveY * -1;
            }
            else if (command.lastIndexOf("ISFOLLOW:", 0) === 0) {
                this.isFollow = command.substring("ISFOLLOW:".length);
                this.isFollow = this.isFollow === "TRUE"
            }
            else if (command.lastIndexOf("GROUP:", 0) === 0) {
                this.group = command.substring("GROUP:".length);
            }
        }

        if (this.eventID != null && this.isFollow == false) {
            var event = this.findEvent();
            this.startX = event._realX;
            this.startY = event._realY;
            this.adjustY = event.shiftY() + event.jumpHeight();
        }

        this.delete = false;

        DataManager.___simpleText___.getSimpleTextObjListByMapID().push(this);
    }

    //add sprite when creating text or starting scene
    SimpleTextObj.prototype.addSprite = function(spriteset_Map, x, y) {
        var sprite = new Sprite();
        sprite.anchor.x = 0.5;
        sprite.anchor.y = 1;
        sprite.z = 9;
        sprite.bitmap = new Bitmap(this.fontSize * this.text.length + this.outlineWidth * 2 + 6, this.fontSize + this.outlineWidth * 2 + 6);
        sprite.bitmap.fontFace = this.fontFace;
        sprite.bitmap.fontSize = this.fontSize;
        sprite.bitmap.fontItalic = this.fontItalic;
        sprite.bitmap.textColor = this.textColor;
        sprite.bitmap.outlineColor = this.outlineColor;
        sprite.bitmap.outlineWidth = this.outlineWidth;
        sprite.bitmap.drawText(this.text, 0, 0, sprite.bitmap.width, sprite.bitmap.height, "center");
        sprite.opacity = this.opacity;
        sprite.___simpleTextObjRef___ = this;

        spriteset_Map._tilemap.addChild(sprite);

        sprite.x = x;
        sprite.y = y;

        return sprite;
    };

    //get event
    SimpleTextObj.prototype.findEvent = function() {
        if ($gameParty.inBattle()) {
            return null;
        }
        else if (this.eventID == null) {
            return null;
        }
        else if (this.eventID < 0) {
            return $gamePlayer;
        }
        else {
            return $gameMap.event(this.eventID);
        }
    }

    //update function
    SimpleTextObj.prototype.update = function(index, spriteset_Map) {
        var event = null;
        var found = false;
        var child = null;

        //if event is null
        if (this.delete == false) {
            if (this.eventID != null) {
                event = this.findEvent();
                if (event == null) {
                    this.delete = true;
                }
            }
        }

        //if duration not null
        if (this.delete == false) {
            if (this.duration != null) {
                this.duration--;
                if (this.duration <= 0) {
                    this.delete = true;
                }
            }
        }

        for (var i = spriteset_Map._tilemap.children.length - 1; i >= 0; i--) {
            child = spriteset_Map._tilemap.children[i];

            if (typeof(child.___simpleTextObjRef___)==='undefined') {
                continue;
            }
            else {
                if (child.___simpleTextObjRef___ == this) {
                    found = true;

                    if (this.delete == true) {
                        child.bitmap = null;
                        spriteset_Map._tilemap.removeChildAt(i);
                    }

                    break;
                }
            }
        }

        if (this.delete == true) {
            DataManager.___simpleText___.getSimpleTextObjListByMapID().splice(index, 1);
            return;
        }

        //get center position
        var centerX = 0;
        var centerY = 0;

        if (event == null) {
            centerX = Graphics.width / 2;
            centerY = Graphics.height / 2;
        }
        else {
            if (this.isFollow == false) {
                var scrolledX = $gameMap.adjustX(this.startX);
                var scrolledY = $gameMap.adjustY(this.startY);

                var tw = $gameMap.tileWidth();
                var screenX = Math.round(scrolledX * tw + tw / 2);

                var th = $gameMap.tileHeight();
                var screenY = Math.round(scrolledY * th + th - this.adjustY);

                centerX = screenX;
                centerY = screenY - $gameMap.tileHeight();
            }
            else {
                centerX = event.screenX();
                centerY = event.screenY() - $gameMap.tileHeight();
            }
        }

        centerX += this.offsetX;
        centerY += this.offsetY;

        if (found == false) {
            child = this.addSprite(spriteset_Map, centerX, centerY);
        }
        else {
            child.x = centerX;
            child.y = centerY;
        }
   
        //update offsets
        this.offsetX += this.moveX;
        this.offsetY += this.moveY;
    };

    //create SimpleText in DataManager
    var _DataManager_createGameObjects = DataManager.createGameObjects;
    DataManager.createGameObjects = function() {
        _DataManager_createGameObjects.call(this);

        if (typeof(DataManager.___simpleText___)==='undefined')
            DataManager.___simpleText___ = new SimpleText();
    };

    //remove sprites on creeate tilemap
    var _Spriteset_Map_prototype_createTilemap = Spriteset_Map.prototype.createTilemap;
    Spriteset_Map.prototype.createTilemap = function() {
        _Spriteset_Map_prototype_createTilemap.call(this);

        //remove all text sprites if exits
        for (var i = this._tilemap.children.length - 1; i >= 0; i--) {
            var child = this._tilemap.children[i];

            if (typeof(child.___simpleTextObjRef___)==='undefined') {
                continue;
            }
            else {
                child.bitmap = null;
                this._tilemap.removeChildAt(i);
            }
        }
    }

    //update SimpleTextObjList
    var _Spriteset_Map_prototype_updateTilemap = Spriteset_Map.prototype.updateTilemap;
    Spriteset_Map.prototype.updateTilemap = function() {
        _Spriteset_Map_prototype_updateTilemap.call(this);

        var simpleTextObjList = DataManager.___simpleText___.getSimpleTextObjListByMapID();
        for (var i = simpleTextObjList.length - 1; i >= 0; i--)
            simpleTextObjList[i].update(i, this);
    };

    //read plugin command
    var _Game_Interpreter_prototype_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_prototype_pluginCommand.call(this, command, args);
       
        //only work with Scene_Map
        if (SceneManager._scene instanceof Scene_Map == false)
            return;

        if (command.toUpperCase() == "SIMPLETEXT") {
            var argsLength = args.length;
            var action = args[0].toUpperCase();
            if (action == "WRITE") {
                if (argsLength == 3) {
                    var simpleTextObj = new SimpleTextObj(args[1], args[2]);
                }
            }
            else if (action == "FONT") {
                if (argsLength == 2) {
                    DataManager.___simpleText___.changeFont(args[1]);
                }
            }
            else if (action == "CLEARALL") {
                for (var mapID in DataManager.___simpleText___.simpleTextObjList) {
                    var simpleTextObjList = DataManager.___simpleText___.simpleTextObjList[mapID];

                    for (var i = simpleTextObjList.length - 1; i >= 0; i--)
                        simpleTextObjList[i].delete = true;
                }
            }
            else if (action == "CLEARMAP") {
                var simpleTextObjList = DataManager.___simpleText___.getSimpleTextObjListByMapID();
                for (var i = simpleTextObjList.length - 1; i >= 0; i--)
                    simpleTextObjList[i].delete = true;
            }
            else if (action == "CLEARBYEVENTID") {
                if (argsLength == 2) {
                    var eventID = parseInt(args[1]);

                    var simpleTextObjList = DataManager.___simpleText___.getSimpleTextObjListByMapID();
                    for (var i = simpleTextObjList.length - 1; i >= 0; i--) {
                        var simpleTextObj = simpleTextObjList[i];
                        if (simpleTextObj.eventID === eventID)
                            simpleTextObj.delete = true;
                    }
                }
            }
            else if (action == "CLEARBYGROUP") {
                if (argsLength == 2) {
                    var group = args[1].toUpperCase();

                    var simpleTextObjList = DataManager.___simpleText___.getSimpleTextObjListByMapID();
                    for (var i = simpleTextObjList.length - 1; i >= 0; i--) {
                        var simpleTextObj = simpleTextObjList[i];
                        if (simpleTextObj.group === group)
                            simpleTextObj.delete = true;
                    }
                }
            }
        }
    };
})();
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,971
Reaction score
13,604
First Language
English
Primarily Uses
RMMV
Hi, can you please add your terms of use (who to credit, can it be used commercially, etc?) and put your code into spoiler tags so it doesn't take up so much space?
 
Joined
Jun 5, 2013
Messages
459
Reaction score
85
First Language
English
Primarily Uses
N/A
Pretty important thing you may not have noticed, this plugin causes save files to take up a lot of space and it is impossible to save. My friend put it in his game and he couldn't save.
 

www

Villager
Member
Joined
Nov 6, 2015
Messages
5
Reaction score
1
First Language
Chinese
Pretty important thing you may not have noticed, this plugin causes save files to take up a lot of space and it is impossible to save. My friend put it in his game and he couldn't save.
Thanks for your feedback. Still looking for a solution. Sorry for any inconvenience caused.
 
Last edited by a moderator:

www

Villager
Member
Joined
Nov 6, 2015
Messages
5
Reaction score
1
First Language
Chinese
:)
 
Last edited by a moderator:

www

Villager
Member
Joined
Nov 6, 2015
Messages
5
Reaction score
1
First Language
Chinese
Version 1.2

- code has been rewritten to perform better
- game saving issue fixed
- sliding text will not follow event anymore

- fixed text shaking issue while scrolling

Version 1.3

- no more creating new objects in every update
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,971
Reaction score
13,604
First Language
English
Primarily Uses
RMMV
www, please avoid double posting, as it is against the forum rules. You can review our forum rules here. Thank you.


Note that bumping is also not allowed unless it has been 72 hours since your last post, and there have been no replies in that time.


Please just edit your first post instead.
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts


Finally! I've been puzzling over why it wasn't working for two days now. Turns out all of my calculations were correct, I just forgot to give some events hitboxes. :kaoswt: Now my randomly generated maps check for collision errors, and move stuff around in real time if they finds any. :LZSexcite:
Ami
--- OP Candy ---

Kids: Trick Or Treat!
Hero: Alright,here's the Candies (Give A Bunch Of Candy).
Kids: Thank you,Mister! (Running)
Hero: How cute they are…
F.Archer: Did you give them a OP Candy,accidently?
Hero: Huh?
Halloween, a day where kids are encouraged to take candy from strangers.
Stream will be live shortly with a session of the Interactive Text Adventure! Feel free to drop by!
Took a break from spriting dinosaurs to carve a dinosaur pumpkin.

Forum statistics

Threads
104,672
Messages
1,007,989
Members
136,161
Latest member
vendomis
Top