From ad86c1a7a9c9f60ce0f3dc52519892f1b26a86e0 Mon Sep 17 00:00:00 2001
From: Myles Metzler <mdmetzle@gmail.com>
Date: Sun, 25 Feb 2018 00:21:42 -0500
Subject: [PATCH] Move legend from key function to legend function.

legend function may be called repeatedly, with posititioning and sizing parameters to place multiple legends on a key.
---
 keys.scad                    |  3 +++
 src/key.scad                 | 20 ++++++++++++--------
 src/key_transformations.scad |  7 +++++++
 src/settings.scad            |  6 ++++++
 4 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/keys.scad b/keys.scad
index 114276f..67e216b 100644
--- a/keys.scad
+++ b/keys.scad
@@ -38,3 +38,6 @@ for (p = [0:len(key_profiles)-1]) {
 }
 
 /* translate_u(0, 0) one_row_profile("oem"); */
+
+dcs_row(3) cherry() legend("X", "top", "left", 3) legend("X") key(true);
+translate_u(1, 0) dcs_row(3) cherry() legend("M", "top", "left", 3) legend("Z") key(true);
diff --git a/src/key.scad b/src/key.scad
index e493668..bf3202b 100644
--- a/src/key.scad
+++ b/src/key.scad
@@ -161,10 +161,12 @@ module top_of_key(){
 	}
 }
 
-module keytext(text, depth = 0) {
-	translate([0, 0, -depth]){
+module keytext(text, halign, valign, font_size, depth) {
+    woffset = (top_total_key_width()/3.5) * (halign == "center" ? 0 : halign == "left" ? -1 : 1);
+    hoffset = (top_total_key_height()/3.5) * (valign == "center" ? 0 : valign == "bottom" ? -1 : 1);
+	translate([woffset, hoffset, -depth]){
 		linear_extrude(height=$dish_depth){
-			text(text=text, font=$font, size=$font_size, halign="center", valign="center");
+			text(text=text, font=$font, size=font_size, halign="center", valign="center");
 		}
 	}
 }
@@ -217,10 +219,12 @@ module clearance_check() {
 }
 
 // legends / artisan support
-module artisan(legend, depth) {
+module artisan(depth) {
 	top_of_key() {
 		// outset legend
-		if (legend != "") keytext(legend, depth);
+        for (i=[0:len($legends)-1]) {
+            keytext($legends[i][0], $legends[i][1], $legends[i][2], $legends[i][3], depth);
+        }
 		// artisan objects / outset shape legends
 		children();
 	}
@@ -244,19 +248,19 @@ module keytop() {
 
 // The final, penultimate key generation function.
 // takes all the bits and glues them together. requires configuration with special variables.
-module key(legend = "", inset = false) {
+module key(inset = false) {
 	difference() {
 		union(){
 			// the shape of the key, inside and out
 			keytop();
 			// additive objects at the top of the key
-			if(!inset) artisan(legend) children();
+			if(!inset) artisan() children();
 			// render the clearance check if it's enabled, but don't have it intersect with anything
 			if ($clearance_check) %clearance_check();
 		}
 
 		// subtractive objects at the top of the key
-		if (inset) artisan(legend, 0.3) children();
+		if (inset) artisan(0.3) children();
 		// subtract the clearance check if it's enabled, letting the user see the
 		// parts of the keycap that will hit the cherry switch
 		if ($clearance_check) clearance_check();
diff --git a/src/key_transformations.scad b/src/key_transformations.scad
index 76ecbf8..8adab57 100644
--- a/src/key_transformations.scad
+++ b/src/key_transformations.scad
@@ -100,3 +100,10 @@ module flat_support() {
   $support_type = "flat";
   children();
 }
+
+module legend(text, valign="center", halign="center", size=0) {
+    //valign = "top" or "center" or "bottom"
+    //halign = "left" or "center" or "right"
+    $legends = [for(L=[$legends, [[text, halign, valign, size > 0 ? size : $font_size]]], a=L) a];
+    children();
+}
diff --git a/src/settings.scad b/src/settings.scad
index 7fc90b3..4876c56 100644
--- a/src/settings.scad
+++ b/src/settings.scad
@@ -109,3 +109,9 @@ $rounded_cherry_stem_d = 5.5;
 
 // dimensions of alps stem
 $alps_stem = [4.45, 2.25];
+
+//list of legends to place on a key format: [text, halign, valign, size]
+//halign = "left" or "center" or "right"
+//valign = "top" or "center" or "bottom"
+$legends = [];
+