Skip to content

Instantly share code, notes, and snippets.

@davidbmaier
Created February 22, 2022 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidbmaier/2c2504d5e3aa86fa0e4f760381a924ed to your computer and use it in GitHub Desktop.
Save davidbmaier/2c2504d5e3aa86fa0e4f760381a924ed to your computer and use it in GitHub Desktop.
Drag blocks - TM2020 editor plugin by Nerpson
/**
* @author Nicolas 'Nerpson' Jullien
*
* License: Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
* You are free to:
*
* Share — copy and redistribute the material in any medium or format
* Adapt — remix, transform, and build upon the material
*
* Under the following terms:
*
* Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
*
* NonCommercial — You may not use the material for commercial purposes.
*
* ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
*/
#RequireContext CMapEditorPlugin
#Include "MathLib" as ML
Void Select(Int3 _From, Int3 _To) {
CustomSelectionCoords.clear();
declare Integer FromX = ML::Min(_From.X, _To.X);
declare Integer FromY = ML::Min(_From.Y, _To.Y);
declare Integer FromZ = ML::Min(_From.Z, _To.Z);
declare Integer ToX = ML::Max(_From.X, _To.X);
declare Integer ToY = ML::Max(_From.Y, _To.Y);
declare Integer ToZ = ML::Max(_From.Z, _To.Z);
log(Now ^ "> " ^ <FromX, FromY, FromZ> ^ "\t" ^ <ToX, ToY, ToZ>);
for (X, FromX, ToX) {
for (Y, FromY, ToY) {
for (Z, FromZ, ToZ) {
CustomSelectionCoords.add(<X, Y, Z>);
}
}
}
}
main() {
declare Boolean SavedMouseLeftButton;
declare Integer CurrentMode = 0;
declare CBlockModel SavedBlockModel;
declare Int3 StartCoord;
while(True) {
switch (CurrentMode) {
case 0: {
if (PlaceMode == ::PlaceMode::Block) {
if (
Input.IsKeyPressed(65) && Input.IsKeyPressed(28)
) {
CurrentMode = 1;
log(Now ^ "> Entering selection mode");
SavedBlockModel = Cursor.BlockModel;
PlaceMode = ::PlaceMode::CustomSelection;
ShowCustomSelection();
CustomSelectionRGB = <0., 0.5, 1.>;
}
}
}
case 1: {
if (Input.MouseLeftButton) {
if (PlaceMode != ::PlaceMode::CustomSelection) PlaceMode = ::PlaceMode::CustomSelection;
if (!SavedMouseLeftButton) {
// Starting selection.
StartCoord = Cursor.Coord;
}
Select(StartCoord, Cursor.Coord);
} else {
if (SavedMouseLeftButton) {
// Stopping selection.
CurrentMode = 0;
log(Now ^ "> Selection finished");
foreach (Coord in CustomSelectionCoords) {
PlaceBlock(SavedBlockModel, Coord, Cursor.Dir);
}
CustomSelectionCoords.clear();
HideCustomSelection();
PlaceMode = ::PlaceMode::Block;
} else if (PlaceMode != ::PlaceMode::CustomSelection) {
// Abandonning selection.
CurrentMode = 0;
log(Now ^ "> Selection abandonned");
}
}
}
}
SavedMouseLeftButton = Input.MouseLeftButton;
yield;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment