GameMaker Studio 2 Click-Drag Tile Selection - multipleselection

I would like to implement a simple way to select a section of a tilemap, though I'm stuck on the math of updating the selected area as you drag.
I spent about 12 hours reasearching and trying different things to see if I can get this to work correctly. I didn't think this would be as tough as it is.
My prefered method of implementing the selection, is using four variables:
selection_x - X position of the top left corner of the selection
selection_y - Y position of the top left corner of the selection
selection_w - Width of the selection
selection_h - Height of the selection
The scrips tilex() and tiley() return the tile X/Y where the mouse is, calculating for zoom, and view offset.
Here is my attempt:
if(mouse_check_button_pressed(mb_left)) {
selecting = true;
selection_start = [tilex(), tiley()];
selection_stop = [tilex() + 1, tiley() + 1];
selection_x = tilex();
selection_y = tiley();
selection_w = 0;
selection_h = 0;
} else if(mouse_check_button(mb_left)) {
var old = selection_x;
selection_x = tilex() >= selection_x + selection_w ? tilex() : selection_x;
selection_w = tilex() >= selection_x + selection_w ? tilex() - selection_x : (old - selection_x);
var old = selection_y;
selection_y = tiley() >= selection_y + selection_h ? tiley() : selection_y;
selection_h = tiley() >= selection_y + selection_h ? tiley() - selection_y : (old - selection_y);
/*
if(selection_x >= tilex()) {
var old = selection_x;
selection_x = tilex();
selection_w += old - selection_x ;
selection_stop[0] = tilex() + 1;
show_debug_message("x 0");
} else if(selection_x + selection_w <= tilex()) {
selection_w = tilex() - selection_x + 1;
selection_start[0] = tilex();
show_debug_message("x 1");
}
if(selection_y >= tiley()) {
var old = selection_y;
selection_y = tiley();
selection_h += old - selection_y;;
selection_stop[1] = tiley() + 1;
show_debug_message("y 0");
} else if(selection_y + selection_h <= tiley()) {
selection_h = tiley() - selection_y + 1;
selection_start[1] = tiley();
show_debug_message("y 1");
}
*/
/*
if(selection_x > tilex() && selection_y > tiley()) {
} else if(selection_x < tilex() && selection_y < tiley()) {
selection_x = tilex();
selection_y = tiley();
selection_w = selection_x - tilex();
selection_h = selection_y - tiley();
}
*/
/*
if(selection_start[0] > selection_stop[0] || selection_start[1] < selection_stop[1]) {
//selection_start = [tilex(), tiley()];
} else {
selection_stop = [tilex() + 1, tiley() + 1];
}
*/
}
Thanks for reading! Hope this can get resolved soon.
- Searous <3

Related

how to use modifying for-loop in Swift 3.2?

I have a converted for-loop in Swift 3.2 that looks similar to this:
for var i in 0..<char.characters.count {
if(self.characters.count > len && ((currentIndex + length2323) < length))
{
i = i - 1
}
}
But, It doesn't work properly. I want to continue loop when set value for i is i = i - 1 but this code getting out of loop
And my previous Swift 2 code is :
for(var i = 0 ; i < char.characters.count ; i += 1) {
if(self.characters.count > len && ((currentIndex + length2323) < length))
{
i = i - 1
}
}
for (index, item) in char.enumerated()
{
//your loop
}
Swift 4 syntax
import UIKit
var char = "char"
var len = 9
var currentIndex = 1
var length2323 = 2323
var length = 17
for var i in 0..<char.count {
if (self.count > len) && ((currentIndex + length2323) < length) {
i = i - 1
}
}
Swift 3.2 syntax
import UIKit
var char = "char"
var len = 9
var currentIndex = 1
var length2323 = 2323
var length = 17
for var i in 0..<char.characters.count {
if (self.characters.count > len) && ((currentIndex + length2323) < length) {
i = i - 1
}
}

Java Printable not printing multiple pages

My print function could only print one page so I tried changing it to print multiple pages. I have now changed my code to print two pages. But the second page is always a blank page.
So on one page there can be 27 "tasks" printed. There are 3 lines each page and each line containing 9 tasks. I have tried changing the if statement in order to get it to print a second page but couldn't get it to work.
if ((i < 9) || ((i>=3*9) && (i<4*9)))
else if ((i < 2*9) || ((i>=4*9) && (i<5*9)))
else if ((i < 3*9) || ((i>=5*9) && (i<6*9))){
Example Code (excuse the poorly written code):
public class Printer implements Printable {
private ArrayList<Task> dataTasks;
private ArrayList<Tag> dataTags;
private int tasksPerPage = 27;
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex * tasksPerPage >= dataTasks.size()) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
Font titleFont = new Font("Courier New", Font.PLAIN, 12);
g2d.setFont(titleFont);
for (int i = tasksPerPage * pageIndex; i < dataTasks.size() && i < tasksPerPage * (pageIndex + 1); i++){
if ((i < 9) || ((i>=3*9) && (i<4*9))){
g.drawString("Task: " + dataTasks.get(i).getName(), 10, 10 + i * 90);
g.drawString("Created: " + dataTasks.get(i).getCreationDate().toString(true,false), 10, 22 + i * 90);
g.drawString("Deadline: " + dataTasks.get(i).getDeadline().toString(true,true), 10, 34 + i * 90);
g.drawString("Compl.: " + dataTasks.get(i).getCompletionDate().toString(true,true),10,46+i*90);
if (dataTasks.get(i).gettagIdList().size() == 0){
g.drawString("Tags: ", 10, 46 + i * 90);
} else {
for (int j = 0; j < dataTasks.get(i).gettagIdList().size(); j++) {
for (int k = 0; k < dataTags.size(); k++) {
if (dataTasks.get(i).gettagIdList().get(j) == dataTags.get(k).getId()){
if (j == 0){
g.drawString("Tags: " + dataTags.get(k).getName(), 10, 58 + i * 90);
} else {
g.drawString(" " + dataTags.get(k).getName(), 10, 58 + i * 90 + j * 10);
}
}
}
}
}
} else if ((i < 2*9) || ((i>=4*9) && (i<5*9))){
int move1 = 202;
g.drawString("Task: " + dataTasks.get(i).getName(), move1, 10 + (i-9) * 90);
g.drawString("Created: " + dataTasks.get(i).getCreationDate().toString(true,false), move1, 22 + (i-9) * 90);
g.drawString("Deadline: " + dataTasks.get(i).getDeadline().toString(true,true), move1, 34 + (i-9) * 90);
g.drawString("Compl.: "+dataTasks.get(i).getCompletionDate().toString(true,true),move1,46+(i-9)*90);
if (dataTasks.get(i).gettagIdList().size() == 0){
g.drawString("Tags: ", move1, 46 + (i-9) * 90);
} else {
for (int j = 0; j < dataTasks.get(i).gettagIdList().size(); j++) {
for (int k = 0; k < dataTags.size(); k++) {
if (dataTasks.get(i).gettagIdList().get(j) == dataTags.get(k).getId()){
if (j == 0){
g.drawString("Tags: " + dataTags.get(k).getName(), move1, 58 + (i-9) * 90);
} else {
g.drawString(" " + dataTags.get(k).getName(), move1, 58 + (i-9) * 90 + j * 10);
}
}
}
}
}
} else if ((i < 3*9) || ((i>=5*9) && (i<6*9))){
int move2y = 392;
int move2x = (i-18) * 90;
g.drawString("Task: " + dataTasks.get(i).getName(), move2y, 10 + move2x);
g.drawString("Created: " + dataTasks.get(i).getCreationDate().toString(true,false), move2y, 22 + move2x);
g.drawString("Deadline: " + dataTasks.get(i).getDeadline().toString(true,true), move2y, 34 + move2x);
g.drawString("Compl.: "+dataTasks.get(i).getCompletionDate().toString(true,true),move2y,46+move2x);
if (dataTasks.get(i).gettagIdList().size() == 0){
g.drawString("Tags: ", move2y, 46 + move2x);
} else {
for (int j = 0; j < dataTasks.get(i).gettagIdList().size(); j++) {
for (int k = 0; k < dataTags.size(); k++) {
if (dataTasks.get(i).gettagIdList().get(j) == dataTags.get(k).getId()){
if (j == 0){
g.drawString("Tags: " + dataTags.get(k).getName(), move2y, 58 + move2x);
} else {
g.drawString(" " + dataTags.get(k).getName(), move2y, 58 + move2x + j * 10);
}
}
}
}
}
}
}
return PAGE_EXISTS;
}
I have tried doing it as the following post: Print more than one page with Printable on Java

Limit lines and characters per line in textarea

After looking at many solutions, I got the following solutions that does exactly what I want.
SOLUTION 1 : works well except it does not work in IE(11)
I will much appreciate if someone can help me out fixing this for IE.
code taken from :https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement
function checkRows(oField, oKeyEvent) {
var nKey = (oKeyEvent || /* old IE */ window.event || /* check is not supported! */ { keyCode: 38 }).keyCode,
// put here the maximum number of characters per line:
nCols = 30,
// put here the maximum number of lines:
nRows = 5,
nSelS = oField.selectionStart, nSelE = oField.selectionEnd,
sVal = oField.value, nLen = sVal.length,
nBackward = nSelS >= nCols ? nSelS - nCols : 0,
nDeltaForw = sVal.substring(nBackward, nSelS).search(new RegExp("\\n(?!.{0," + String(nCols - 2) + "}\\n)")) + 1,
nRowStart = nBackward + nDeltaForw,
aReturns = (sVal.substring(0, nSelS) + sVal.substring(nSelE, sVal.length)).match(/\n/g),
nRowEnd = nSelE + nRowStart + nCols - nSelS,
sRow = sVal.substring(nRowStart, nSelS) + sVal.substring(nSelE, nRowEnd > nLen ? nLen : nRowEnd),
bKeepCols = nKey === 13 || nLen + 1 < nCols || /\n/.test(sRow) || ((nRowStart === 0 || nDeltaForw > 0 || nKey > 0) && (sRow.length < nCols || (nKey > 0 && (nLen === nRowEnd || sVal.charAt(nRowEnd) === "\n"))));
return (nKey !== 13 || (aReturns ? aReturns.length + 1 : 1) < nRows) && ((nKey > 32 && nKey < 41) || bKeepCols);
}
<form>
<p>Textarea with fixed number of characters per line:<br />
<textarea cols="50" rows="10" onkeypress="return checkRows(this, event);" onpaste="return false;" /></textarea></p>
</form>
SOLUTION 2
It works in IE but it fails in when editing the lines. You type a line, go back using left arrow keys and type you can type 1 letter and the cursor goes back to the end.
code taken from : http://cgodmer.com/?p=55 that
//limit # of lines of a text area and length of those lines
//<textarea rows="4" chars="40" onkeyup="limitTextareaLine(this, event)" ></textarea>
//Author: CGodmer (Feb 22, 2012)
function limitTextareaLine(x, e, nRows, nChars) {
var rows = $(x).val().split("\n").length; //number of rows
var lineCharLimit = nChars; //number of characters to limit each row to
var rowLimit = nRows; //number of rows to allow
//limit length of lines
for (var i = 0; i < rows; i++) {
var rowLength = $(x).val().split("\n")[i].length;
//check to see if any of the rows have a length greater than the limit
if (rowLength > lineCharLimit) {
//if it does save the beg index of the row
var rowstartindex = $(x).val().indexOf($(x).val().split("\n")[i]);
//use the index to get a new value w/ first lineCharLimit number of characters
var newval = $(x).val().substring(0, rowstartindex + lineCharLimit)
+ $(x).val().substring(rowstartindex + rowLength, $(x).val().length);
//replace that value in the textarea
$(x).val(newval);
//set character position back to end of the modified row
setCaretPosition($(x)[0], rowstartindex + lineCharLimit);
}
}
//limit # of lines to limit to is rows attribute
while($(x).val().split("\n").length > rowLimit) {
$(x).val($(x).val().substring(0, $(x).val().length - $(x).val().split("\n")[rowLimit].length - 1));
}
}
//Set caret position in the supplied control to position
//From: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
function setCaretPosition(ctrl, pos) {
if (ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos, pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea rows="5" cols="10" onkeyup="limitTextareaLine(this, event, 5, 10)" ></textarea>
Try this one, it may solve this problem.
function ValidationAddress() {
var allText;
allText = document.getElementById("<%= txtAdd1.ClientID %>").value;
allText = document.getElementById('<%=txtAdd1.ClientID%>').value;
var A = allText.split('\n');
var L = A.length;
if (L > 3 && event.keyCode != 8 && event.keyCode != 46) {
alert("You have exceeded maximum limit.Cannot insert more than 3 lines.");
valert = false;
return false;
}
var arr = allText.split("\n");
for (var i = 0; i < arr.length; i++) {
if (arr[i].length > 10) {
alert("You have exceeded the Maximum Limit..Characters per line is 70 in Address Field !");
valert = false;
return false;
}
}
}

Optimizing Performance for Level Generation

I'm generating simple game maps using a method described in this post. I'm using the second method of starting with a seed tile and randomly growing out. I'm testing it in a Swift playground on a 30x30 array. The problem is my method takes several minutes to generate a 30x30 map, and maps in my real game might exceed 100x100 tiles. How can I optimize the following code to run faster?
import UIKit
import SpriteKit
var map = [[String]](count: 30, repeatedValue:[String](count: 30, repeatedValue:" "))
var landTiles = [(y: Int,x: Int)]()
landTiles.append((15,x: 15))
let minX = 3
let maxX = 28
let minY = 3
let maxY = 28
var coastTiles = [(y: Int,x: Int)]()
var hasReachedBounds = false
while !hasReachedBounds {
coastTiles = []
for tile in landTiles {
let coastal = isCoastal(tile.y, x: tile.x)
if coastal {
coastTiles.append(tile)
}
}
let numTiles = UInt32(coastTiles.count)
print(map)
let randomTile = coastTiles[Int(arc4random_uniform(numTiles))]
if randomTile.x <= minX || randomTile.x >= maxX || randomTile.y <= minY || randomTile.y >= maxY {
hasReachedBounds = true
break
}
let coastal = isCoastal(randomTile.y, x: randomTile.x)
if !coastal{
continue
}
let randomPosition = arc4random_uniform(4) + 1
switch randomPosition {
case 1:
landTiles.append((randomTile.y + 1, randomTile.x))
map [randomTile.y + 1][randomTile.x] = "&"
case 2:
landTiles.append((randomTile.y, randomTile.x + 1))
map [randomTile.y][randomTile.x + 1] = "&"
case 3:
landTiles.append((randomTile.y - 1, randomTile.x))
map [randomTile.y - 1][randomTile.x] = "&"
case 4:
landTiles.append((randomTile.y, randomTile.x - 1))
map [randomTile.y][randomTile.x - 1] = "&"
default: break
}
}
func isCoastal (y: Int, x: Int) -> Bool{
if map[y + 1][x] == " " || map[y][x + 1] == " " || map[y - 1][x] == " " || map[y][x - 1] == " "{
return true
} else {
return false
}
}
print(map)

ActionScript Unexpected Slashes, Parenthesis, and Squiggly-brackets?

This ActionScript code I have been working on for a few days now works 100% just fine in JavaScript, but when I try to compile it in ActionScript it says I have unexpected /, ), and } symbols. Is this syntax wrong and if so how should I fix it? I figured I could test it as Javascript for quicker testing using http://jsfiddle.net/ but now I'm like =(
var txt = "This is a [rainbow]test to show that I can[/rainbow] make whatever I want [rainbow]appear as a rainbow[/rainbow] because I am [rainbow]awesome[/rainbow].";
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
var colors = ['f0f', 'f0c', 'f09', 'f06', 'f03', 'f00', 'f30', 'f60', 'f90', 'fc0', 'ff0', 'cf0', '9f0', '6f0', '3f0', '0f0', '0f3', '0f6', '0f9', '0fc', '0ff', '0cf', '09f', '06f', '03f', '00f', '30f', '60f', '90f', 'c0f'];
function rainbowify(text) {
return text.replace(/\[rainbow\](.+?)\[\/rainbow\]/g, function(_, inner) {
return inner.replace(/./g, function(ch, i) {
return '<font color="#' + colors[i % colors.length] + '">' + ch + '</font>';
});
})
}
txt = rainbowify(txt);
document.write(txt);
}​
Well, this is it:
txt = txt.replace("&apos;", "#");
if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
var firstChar = txt.indexOf("[rainbow]") + 9;
var lastChar = txt.indexOf("[/rainbow]");
while (lastChar <= txt.lastIndexOf("[/rainbow]")) {
var RAINBOWTEXT = '';
var i = firstChar;
while (i < lastChar) {
RAINBOWTEXT += txt.charAt(i);
i++
}
var text = RAINBOWTEXT;
var texty = '';
colors = new Array('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000','ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00','99ff00','66ff00','33ff00','00ff00','00ff33','00ff66','00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff','0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');
i = 0;
while (i <= text.length) {
var t = text.charAt(i);
if (t != undefined) {
texty += "<font color=\"#" + colors[i % colors.length] + "\">" + t + "</font>";
i++;
}
}
texty = texty.replace("> <", "> <");
var REPLACEME = "[rainbow]" + RAINBOWTEXT + "[/rainbow]";
txt = txt.replace(REPLACEME, texty);
if (lastChar == txt.lastIndexOf("[/rainbow]")) {
break;
}
nextChar = lastChar + 10;
firstChar = txt.indexOf("[rainbow]", lastChar) + 9;
lastChar = txt.indexOf("[/rainbow]", lastChar);
}
}
txt = txt.replace("#", "&apos;");

Resources